I would like to use the data.table
package in R
to calculate column means for many columns by another set of columns. I know how to do this for a few columns, and I provide an example below. However, in my non-toy example, I have tens of variables I would like to do this for, and I would like to find a way to do this from a vector of the column names. Is this possible?
library(data.table)
# creates data table
dfo <- data.frame(bananas = 1:5,
melonas = 6:10,
yeah = 11:15,
its = c(1,1,1,2,2)
)
dto <- data.table(dfo)
# gets column means by 'its' column
dto[,
.('bananas_mean' = mean(bananas),
'melonas_mean' = mean(melonas),
'yeah_mean' = mean(yeah)
),
by = .(its)]