I want the same results as in R summarizing multiple columns with data.table but for several summary functions.
Here is an example
data <- as.data.table(list(x1 = runif(200), x2 = 10*runif(200), group = factor(sample(letters[1:2]))))
res <- data[, rbindlist(lapply(.SD, function(x) {
return(list(name = "varname", mean = mean(x), sd = sd(x)))
}))
, by = group, .SDcols = c("x1", "x2")
]
And get the following result:
group name mean sd
1: b varname 0.5755798 0.2723767
2: b varname 5.5108886 2.7649262
3: a varname 0.4906111 0.3060961
4: a varname 4.7780189 2.9740149
How can I get column names ('x1', 'x2') in second column? I guess that I need to substitute rbindlist
to something else, but what? Is there any simple solution?