I am trying to summarize a data.table using a character variable as the name for the new column along with by
.
library(data.table)
dt <- data.table(g = rep(1:3, 4), xa = runif(12), xb = runif(12))
# desired output
dt[, .(sa = mean(xa)), by = g]
g sa
1: 1 1.902360
2: 2 2.149041
3: 3 2.586044
The issue is that the following code returns the entire data.table still, without reducing to just the unique values of g.
cn <- paste0('s', 'a')
# returns all rows
dt[, (cn) := mean(xa), by = g][]
g xa xb sa
1: 1 0.3423699 0.81447505 0.4755900
2: 2 0.0932055 0.06853225 0.5372602
3: 3 0.2486223 0.13286546 0.6465111
4: 1 0.6942175 0.66405944 0.4755900
5: 2 0.7225208 0.83110248 0.5372602
6: 3 0.9898293 0.09520907 0.6465111
7: 1 0.3523753 0.72743182 0.4755900
8: 2 0.5504942 0.01966303 0.5372602
9: 3 0.3523625 0.55257436 0.6465111
10: 1 0.5133974 0.39650089 0.4755900
11: 2 0.7828203 0.89909528 0.5372602
12: 3 0.9952302 0.16872205 0.6465111
How do I get the usual summarized data.table? (This is a simplified example. In my actual problem, there will be multiple names passed to a loop)