<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Hi Vincent,</div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">Have you checked out the special variable `.I`? Have a look at `?data.table`. This SO post may also be relevant: <a href="http://stackoverflow.com/questions/21198937/subset-data-table-using-min-condition/21199009#21199009">http://stackoverflow.com/questions/21198937/subset-data-table-using-min-condition/21199009#21199009</a></div> <div id="bloop_sign_1394456817003480064" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">Arun</div></div> <div style="color:gray"><hr style="style:linear">From: <span style="color:black">Vincent Carey</span> <a href="mailto:stvjc@channing.harvard.edu">Vincent Carey</a><br>Reply: <span style="color:black">Vincent Carey</span> <a href="mailto:stvjc@channing.harvard.edu">stvjc@channing.harvard.edu</a><br>Date: <span style="color:black">March 10, 2014 at 4:33:27 AM</span><br>To: <span style="color:black">datatable-help@lists.r-forge.r-project.org</span> <a href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a><br>Subject: <span style="color:black"> [datatable-help] checking an approach to filtering rows in a data.table <br></span></div> <blockquote type="cite" class="clean_bq"><span><div><div>
<title></title>
<div dir="ltr">
<div>I have looked around for code on row filtering with
data.table, but have</div>
<div>not found anything addressing this use case.</div>
<div><br></div>
<div>I want to retrieve the rows satisfying a certain condition
within groups, in this case having the maximum value for a specific
variable. The following</div>
<div>seems to work, but I wonder if there is a more direct
approach.</div>
<div><br></div>
<div>rowsWmaxVinG = function(dt, V, by) {</div>
<div>#</div>
<div># filter dt to the rows possessing max value of</div>
<div># variable V within groups formed using by</div>
<div>#</div>
<div># example: data(mtcars)</div>
<div># ddt = data.table(mtcars)</div>
<div>#> rowsWmaxVinG( ddt, by="cyl", V="mpg")</div>
<div># mpg cyl disp hp drat
wt qsec vs am gear carb</div>
<div>#1: 33.9 4 71.1 65 4.22 1.835 19.90 1
1 4 1</div>
<div>#2: 21.4 6 258.0 110 3.08 3.215 19.44 1 0
3 1</div>
<div>#3: 19.2 8 400.0 175 3.08 3.845 17.05 0 0
3 2</div>
<div>#</div>
<div> setkeyv(dt, c(by, V)) # sort within groups</div>
<div> dt[ cumsum(dt[, .N, by=by]$N), ] # take last row
from each group</div>
<div>}</div>
</div>
_______________________________________________
<br>datatable-help mailing list
<br>datatable-help@lists.r-forge.r-project.org
<br>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</div></div></span></blockquote></body></html>