Reputation: 10203
I have a data.table and would like to run multiple aggregations on multiple columns while the table is grouped on another variable. I have tried the following:
library(data.table)
DT <- data.table(a = 1:10,
b = 10:1,
group = rep(1:2, each=5))
aggs <- function(x) list(mean = mean(x), sd = sd(x))
DT[, lapply(.SD, aggs), .(group), .SDcols = c('a', 'b')]
This doesn't quite work as I would either need the names()
as a column or for the output to be split into columns - say a.mean
, b.mean
, etc.:
group a b
1: 1 3 8
2: 1 1.581139 1.581139
3: 2 8 3
4: 2 1.581139 1.581139
Upvotes: 1
Views: 94
Reputation: 6267
You were close, missing an extra data.frame
to cast the results the way you want:
DT[, data.frame(lapply(.SD, aggs)), by=group, .SDcols = c('a', 'b')]
giving:
group a.mean a.sd b.mean b.sd
1: 1 3 1.581139 8 1.581139
2: 2 8 1.581139 3 1.581139
Upvotes: 1