<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 Gerald Jean,</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;">Glad to hear it!</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;">The number of elements in each group is contained in a special variable `.N`. IIUC, all you’ve to do is replace ’n’ by ‘.N’ in your DT code.</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;">BTW, we are also adding detailed (HTML) vignettes, which you can find here: <a href="https://github.com/Rdatatable/data.table/wiki/Getting-started">https://github.com/Rdatatable/data.table/wiki/Getting-started</a></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;">You can see here: <a href="https://github.com/Rdatatable/data.table/issues/944">https://github.com/Rdatatable/data.table/issues/944</a> to get an idea on the vignettes that are yet to be done.</div> <div><br></div>Hope this is of some help.<div><br> <div id="bloop_sign_1424116679606680832" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br>Arun</div></div> <br><p style="color:#000;">On 16 Feb 2015 at 20:55:04, Gerald Jean (<a href="mailto:gerald.jean@dgag.ca">gerald.jean@dgag.ca</a>) wrote:</p> <blockquote type="cite" class="clean_bq"><span><div lang="FR-CA" link="blue" vlink="purple" xml:lang="FR-CA"><div></div><div>
<!--[if !mso]><![endif]-->
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<title></title>
<div class="WordSection1">
<p class="MsoNormal">Hello,</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US">I am
fairly new to data.table, it’s fast and I love it!!! Here is
what I am trying to do. Suppose I have a data.table DT, with
columns a, b, c, v, t and g. I want to add a new column, x,
say, where for each group defined by g, in vector
notation:</span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US">x = c(0,
(v[-1] – v[-n]) / (t[-1] – t[-n]))</span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US">where n is
the number of rows for the groups, I don’t know n yet.
Obviously</span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US">DT[, x :=
c(0, (v[-1] – v[-n]) / (t[-1] – t[-n])), by = g]</span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US">won’t
work. I have read the doc I found so far but couldn’t find
examples of subsetting the groups, maybe it could be done using .SD
but I am not familiar enough with data.table yet to figure out how
to do it.</span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US">By the way
my data.tables are large, 50000 to over 1000000 rows and I have
over 60000 of them to process and many more operations to perform,
I just hope data.table will do the trick!!!</span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US">Thanks for
your help,</span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US"> </span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US">Gérald</span></p>
<p class="MsoNormal"><span lang="EN-US" xml:lang="EN-US"> </span></p>
<table class="MsoNormalTable" border="0" cellpadding="0" width="640" style="width:480.0pt">
<tbody>
<tr>
<td style="padding:.75pt .75pt .75pt .75pt">
<p class="MsoNormal"><span style="mso-fareast-language:FR-CA"><img width="136" height="54" id="_x0000_i1025" src="cid:A1A738A1-AA37-40BE-A54D-5F77BE9AA9D6" name="_x0000_i1025"></span></p>
</td>
<td style="padding:.75pt .75pt .75pt .75pt"></td>
<td style="padding:.75pt .75pt .75pt .75pt"></td>
</tr>
<tr>
<td width="300" valign="top" style="width:225.0pt;padding:.75pt .75pt .75pt .75pt">
<p class="MsoNormal"><b><span style="font-size:8.0pt;font-family:"Verdana","sans-serif";color:black;mso-fareast-language:FR-CA">
Gerald Jean, M. Sc. en statistiques</span></b><span style="font-size:8.0pt;font-family:"Verdana","sans-serif";color:black;mso-fareast-language:FR-CA"><br>
Conseiller senior en statistiques<br>
<br>
Actuariat corporatif,<br>
Modélisation et Recherche<br>
Assurance de dommages<br>
Mouvement Desjardins</span></p>
</td>
<td width="170" valign="top" style="width:127.5pt;padding:.75pt .75pt .75pt .75pt">
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Verdana","sans-serif";color:black;mso-fareast-language:FR-CA">
<br>
Lévis (siège social)<br>
<br>
418 835-4900,</span></p>
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Verdana","sans-serif";color:black;mso-fareast-language:FR-CA">
poste 5527639<br>
1 877 835-4900,</span></p>
<p class="MsoNormal"><span style="font-size:8.0pt;font-family:"Verdana","sans-serif";color:black;mso-fareast-language:FR-CA">
poste 5527639<br>
Télécopieur : 418 835-6657</span></p>
</td>
<td width="170" valign="top" style="width:127.5pt;padding:.75pt .75pt .75pt .75pt">
<p class="MsoNormal" style="margin-bottom:12.0pt"><span style="font-size:8.0pt;font-family:"Verdana","sans-serif";color:black;mso-fareast-language:FR-CA">
<br>
<br>
<br>
<br></span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><span style="display:none;mso-fareast-language:FR-CA"> </span></p>
<table class="MsoNormalTable" border="0" cellpadding="0" width="640" style="width:480.0pt">
<tbody>
<tr>
<td style="padding:.75pt .75pt .75pt .75pt">
<p class="MsoNormal"><span style="font-size:7.0pt;font-family:"Verdana","sans-serif";color:black;mso-fareast-language:FR-CA">
Faites bonne impression et imprimez seulement au besoin!<br>
<br></span><span style="font-size:7.0pt;font-family:"Verdana","sans-serif";color:dimgray;mso-fareast-language:FR-CA">Ce
courriel est confidentiel, peut être protégé par le secret
professionnel et est adressé exclusivement au destinataire. Il est
strictement interdit à toute autre personne de diffuser, distribuer
ou reproduire ce message. Si vous l'avez reçu par erreur, veuillez
immédiatement le détruire et aviser l'expéditeur. Merci.</span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><span style="mso-fareast-language:FR-CA"> </span></p>
<p class="MsoNormal"> </p>
</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></div></body></html>