I had a look at your example and for me it works. Note that
pathoffset==max(pathoffset) returns every row for which pathoffset is equal to max(pathoffset) for this particular key/Subj combination. And it just happens that your first combination key==1 & Sub==131 has max(pathoffset)==0 and many rows of with 0 pathoffset.<div>
<br></div><div>If you have for example a look at mx[key==141], you only get back one row:</div><div><br></div><div> Subj wordPos correctResp LiftOff resp RT BeepTargSOA presT key pathoffset</div><div>[1,] 1 0 0 64 1 835 694 448.1333 141 0.10344</div>
<div> xvel Sample cnd</div><div>[1,] 47.68907 156 cong</div><div><br></div><div>So I guess everything is working out here. I hope I didn't miss anything.</div><div><br></div><div>Christoph<br><br><div class="gmail_quote">
On Wed, Jun 6, 2012 at 8:11 AM, Matthew Finkbeiner <span dir="ltr"><<a href="mailto:matthew.finkbeiner@gmail.com" target="_blank">matthew.finkbeiner@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I have a data.table that I am trying to pull, from each subset, the<br>
row with a max column value.<br>
<br>
here is an example:<br>
<br>
> library(data.table)<br>
data.table 1.8.0 For help type: help("data.table")<br>
> tables()<br>
NAME NROW MB COLS<br>
KEY<br>
[1,] one 165,798 15<br>
Subj,wordPos,correctResp,LiftOff,resp,RT,BeepTargSOA,presT,key,pathoffset,xvel,S<br>
Subj,cnd,key<br>
Total: 15MB<br>
> one<br>
Subj wordPos correctResp LiftOff resp RT BeepTargSOA presT<br>
key pathoffset xvel Sample cnd<br>
[1,] 1 0 0 -13 1 657 694 -196.3333<br>
131 -0.002970074 0.2758665 1 cong<br>
[2,] 1 0 0 -13 1 657 694 -192.1667<br>
131 -0.002987193 0.2615092 2 cong<br>
[3,] 1 0 0 -13 1 657 694 -188.0000<br>
131 -0.003003959 0.2463627 3 cong<br>
[4,] 1 0 0 -13 1 657 694 -183.8333<br>
131 -0.003020312 0.2301467 4 cong<br>
[5,] 1 0 0 -13 1 657 694 -179.6667<br>
131 -0.003036162 0.2127339 5 cong<br>
[6,] 1 0 0 -13 1 657 694 -175.5000<br>
131 -0.003051398 0.1941322 6 cong<br>
[7,] 1 0 0 -13 1 657 694 -171.3333<br>
131 -0.003065893 0.1744955 7 cong<br>
[8,] 1 0 0 -13 1 657 694 -167.1667<br>
131 -0.003079516 0.1541903 8 cong<br>
[9,] 1 0 0 -13 1 657 694 -163.0000<br>
131 -0.003092148 0.1338630 9 cong<br>
[10,] 1 0 0 -13 1 657 694 -158.8333<br>
131 -0.003103696 0.1144705 10 cong<br>
First 10 rows of 165798 printed.<br>
<br>
<br>
each subset is defined by unique values in the "Subj" and "key" column<br>
and I use this to retrieve the row with the max "pathoffset" value<br>
from each subset:<br>
<br>
> mx<- one[one[,pathoffset==max(pathoffset),by='Subj,key'][[3]]]<br>
> head(mx)<br>
Subj wordPos correctResp LiftOff resp RT BeepTargSOA presT<br>
key pathoffset xvel Sample cnd<br>
[1,] 1 0 0 -13 1 657 694 657.8333<br>
131 0 1.617281 206 cong<br>
[2,] 1 0 0 -13 1 657 694 657.8333<br>
131 0 1.617281 207 cong<br>
[3,] 1 0 0 -13 1 657 694 657.8333<br>
131 0 1.617281 208 cong<br>
[4,] 1 0 0 -13 1 657 694 657.8333<br>
131 0 1.617281 209 cong<br>
[5,] 1 0 0 -13 1 657 694 657.8333<br>
131 0 1.617281 210 cong<br>
[6,] 1 0 0 -13 1 657 694 657.8333<br>
131 0 1.617281 211 cong<br>
><br>
<br>
The columns are likely to have shifted, so it may not be clear, but<br>
this did not return unique rows. nor does this:<br>
<br>
mx<- one[, .SD[which(pathoffset == max(pathoffset)),], by = list(Subj, key)]<br>
><br>
> head(mx)<br>
Subj key wordPos correctResp LiftOff resp RT BeepTargSOA<br>
presT pathoffset xvel Sample cnd<br>
[1,] 1 131 0 0 -13 1 657 694<br>
657.8333 0 1.617281 206 cong<br>
[2,] 1 131 0 0 -13 1 657 694<br>
657.8333 0 1.617281 207 cong<br>
[3,] 1 131 0 0 -13 1 657 694<br>
657.8333 0 1.617281 208 cong<br>
[4,] 1 131 0 0 -13 1 657 694<br>
657.8333 0 1.617281 209 cong<br>
[5,] 1 131 0 0 -13 1 657 694<br>
657.8333 0 1.617281 210 cong<br>
[6,] 1 131 0 0 -13 1 657 694<br>
657.8333 0 1.617281 211 cong<br>
<br>
<br>
Is this a bug? Or have I done something wrong? I've posted a small<br>
set of the data if that helps. You can get it here:<br>
<a href="http://personal.maccs.mq.edu.au/~mfinkbei/Rdata/one.RData" target="_blank">http://personal.maccs.mq.edu.au/~mfinkbei/Rdata/one.RData</a><br>
<br>
thanks<br>
<br>
Matthew<br>
_______________________________________________<br>
datatable-help mailing list<br>
<a href="mailto:datatable-help@lists.r-forge.r-project.org" target="_blank">datatable-help@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a><br>
</blockquote></div><br><br clear="all"><div><br></div>--<br>
</div>