I know this can be achieved with other packages, but I'm trying to do it in data.table
(as it seems to be the fastest for grouping).
library(data.table)
dt = data.table(a=c(1,2,2,3))
dt[,length(a),by=a]
results in
a V1
1: 1 1
2: 2 1
3: 3 1
whereas
df = data.frame(a=c(1,2,2,3))
ddply(df,.(a),summarise,V1=length(a))
produces
a V1
1 1 1
2 2 2
3 3 1
which is a more sensible results. Just wondering why data.table
is not giving the same results, and how this can be achieved.