Finding both count and average of a column in R data.table, after group by
If you want to get but the sum and length of id with data.table, you can do it like this:

dt[,.(total=sum(id), n=length(id)), by=cid]


You could even write this in shorter format with the .N operator of data.table:

dt[,.(total=sum(id), .N), by=cid]