[datatable-help] Subsetting within groups.

Gerald Jean gerald.jean at dgag.ca
Mon Feb 16 20:54:54 CET 2015


Hello,

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:

x = c(0, (v[-1] - v[-n]) / (t[-1] - t[-n]))

where n is the number of rows for the groups, I don't know n yet.  Obviously

DT[, x :=  c(0, (v[-1] - v[-n]) / (t[-1] - t[-n])), by = g]

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.

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!!!

Thanks for your help,

Gérald

[cid:image001.gif at 01D049F7.A393B320]

Gerald Jean, M. Sc. en statistiques
Conseiller senior en statistiques

Actuariat corporatif,
Modélisation et Recherche
Assurance de dommages
Mouvement Desjardins


Lévis (siège social)

418 835-4900,
poste 5527639
1 877 835-4900,
poste 5527639
Télécopieur : 418 835-6657







Faites bonne impression et imprimez seulement au besoin!

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.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20150216/eb3a939e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.gif
Type: image/gif
Size: 6632 bytes
Desc: image001.gif
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20150216/eb3a939e/attachment.gif>


More information about the datatable-help mailing list