R data. Aggregate data
I have used data aggregation with help aggregate
, how can I do something like this in data.table
?
Let's say I generate data like this:
data <- data.table("a"=sample(1:100, 100),
"b"=sample(1:100, 100),
"c"=sample(1:100, 100),
"d"=sample(1:100, 100),
"metric"=rnorm(100))
I used this: aggregate(metric~a+b, data=data, sum)
. What's the equivalent way to do this in data.table
? I tried data[, total:=sum(metric), by=list(a,b)]
, but other columns are not discarded.
+3
source to share