Data.table rocks! Data manipulation the fast way in R
Here is a simple example: I have a data frame showing incremental claims development by line of business and origin year. Now I would like add a column with the cumulative claims position for each line of business and each origin year along the development years.
It’s one line with
data.table! Here it is:
myData[order(dev), cvalue:=cumsum(value), by=list(origin, lob)]
It is even easy to read! Notice also that I don’t have to copy the data. The operator
‘:=’ works by reference and is one of the reasons why
data.table is so fast.
And it is getting even better. Suppose you want to get the latest claims development position for each line of business and origin year. Again, it is only one line:
latestData <- myData[, .SD[max(dev)] , by=list(origin, lob)]
Here is the R code of the examples above:
R Under development (unstable) (2012-10-19 r60974) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) locale:  en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8 attached base packages:  stats graphics grDevices utils datasets methods base other attached packages:  lattice_0.20-10 data.table_1.8.4 loaded via a namespace (and not attached):  grid_2.16.0