<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Dear all,<div><br></div><div>data.table is great! thanks for this life(time)saving package.</div><div><br></div><div>Now, I run into a difficult nut to crack using ':='.</div><div>I'd like to do a calculation using column information conditional on another column</div><div><br></div><div>first some jumbo data:</div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; "><span style="color: #052499">library(</span>data.table<span style="color: #052499">)</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "><span style="color: #000000">DT </span><- data.table(</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "> <span style="color: #065218">1</span>:<span style="color: #065218">50</span>,</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "> rep(<span style="color: #065218">1</span>:<span style="color: #065218">5</span>,<span style="color: #000000">each</span>=<span style="color: #065218">10</span>),</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "> runif(<span style="color: #065218">50</span>,<span style="color: #065218">0</span>,<span style="color: #065218">1</span>)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); ">)</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); ">setnames(<span style="color: #000000">DT</span>, 1:3, c(<span style="color: #b0140f">"id"</span>,<span style="color: #b0140f">"grp"</span>,<span style="color: #b0140f">"p"</span>))</div></div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "><span style="color: #000000"><font class="Apple-style-span" face="Helvetica" size="3">id's are unique</font></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "><span style="color: #000000"><font class="Apple-style-span" face="Helvetica" size="3">grp's speaks for itself</font></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "><span style="color: #000000"><font class="Apple-style-span" face="Helvetica" size="3">think of p's as e.g. p-values</font></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "><span style="color: #000000"><font class="Apple-style-span" face="Helvetica" size="3"><br></font></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "><span style="color: #000000"><font class="Apple-style-span" face="Helvetica" size="3">next, if I want to obtain the nr of p values at least as extreme as the p of each row </font></span><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; ">from the whole set, this seems to work well:</span><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: Helvetica; font-size: medium; "> </span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "><span style="color: #000000"><font class="Apple-style-span" face="Helvetica" size="3"><br></font></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "><span style="color: #000000">DT</span>[,<span style="color: #000000">c1 </span>:= sum(<span style="color: #000000">DT</span>[,<span style="color: #000000">p</span>] <= <span style="color: #000000">p</span>), <span style="color: #000000">by</span>=<span style="color: #000000">id</span>]</div></div><div><br></div><div>but then, I would like to get the nr of p values at least as extreme as the p of each row for the subset with identical grp, I am having a hard time, because these attempts fail:</div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "><span style="color: #000000">DT</span>[,<span style="color: #000000">c2 </span>:= sum(<span style="color: #000000">DT</span>[<span style="color: #000000">grp</span>,<span style="color: #000000">p</span>] <= <span style="color: #000000">p</span>),<span style="color: #000000">by</span>=<span style="color: #000000">id</span>]</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 4px; text-indent: -4px; font: normal normal normal 11px/normal Monaco; color: rgb(5, 36, 153); "><span style="color: #000000">DT</span>[,<span style="color: #000000">c3 </span>:= sum(<span style="color: #000000">DT</span>[<span style="color: #000000">DT</span>[,<span style="color: #000000">grp</span>]==<span style="color: #000000">grp</span>,<span style="color: #000000">p</span>] <= <span style="color: #000000">p</span>), <span style="color: #000000">by</span>=<span style="color: #000000">id</span>]</div></div><div><br></div><div>What I am after is like the following output examplified by the first 20 rows.</div><div>So that p=0.286 for the first row is compared with the p's of grp=1, the result is 3 because three of these p's are equal or smaller than 0.286 (i.e. 0.286, 0.078, and 0.211).</div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> id grp p c1 c2</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> 1: 1 1 0.28619666 12 3</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> 2: 2 1 0.31725169 14 4</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> 3: 3 1 0.82172331 41 8</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> 4: 4 1 0.07867874 4 1</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> 5: 5 1 0.69134289 35 7</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> 6: 6 1 0.21123476 7 2</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> 7: 7 1 0.39156432 20 5</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> 8: 8 1 0.98862365 50 10</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> 9: 9 1 0.55943136 25 6</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">10: 10 1 0.93543842 47 9</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">11: 11 2 0.48740254 21 5</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">12: 12 2 0.02974435 1 1</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">13: 13 2 0.14566443 6 2</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">14: 14 2 0.23408044 10 3</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">15: 15 2 0.63503196 32 7</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">16: 16 2 0.34114088 16 4</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">17: 17 2 0.56849053 26 6</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">18: 18 2 0.71877039 36 8</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">19: 19 2 0.84100007 42 10</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">20: 20 2 0.81776585 40 9</div></div><div><br></div><div>Any help to solve this issue is greatly appreciated.</div><div><br></div><div>Philip</div></body></html>