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


1 answer


You can filter the result:

data[, total:=sum(metric), by=list(a,b)][, c('a','b','metric'), with=FALSE]

      



Another way to get the same result (I think it's faster) is to use dcast

:

reshape2::dcast(data,a+b~.,fun=sum,value.var="metric")

      

+6


source







All Articles