data.table
Proper/fastest way to reshape a data.table
The data.table package implements faster melt/dcast functions (in C). It also has additional features by allowing to melt and cast multiple columns. Please see the new Efficient reshaping using data.tables on Github. melt/dcast functions for data.table have been available since v1.9.0 and the features include: There is no need to load reshape2 package prior to … Read more
Subset rows corresponding to max value by group using data.table
Here’s the fast data.table way: bdt[bdt[, .I[g == max(g)], by = id]$V1] This avoids constructing .SD, which is the bottleneck in your expressions. edit: Actually, the main reason the OP is slow is not just that it has .SD in it, but the fact that it uses it in a particular way – by calling … Read more