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