I am trying to summarise a datatable as part of a function. The grouping variable changes depending on user requirements/data. The grouping variables are entered as a vector of strings. How can I use this as a grouping variable? See code below
library(data.table)
test <- as.data.table(mtcars)
grouping_vars <- c("vs", "am")
#does what I would like
test[,.(counts = .N,
mpg = mean(mpg),
disp = median(disp)), by = .(vs,am)]
#throws error
test[,.(counts = .N,
mpg = mean(mpg),
disp = median(disp)), by = .(grouping_vars)]
#only gets the first variable
test[,.(counts = .N,
mpg = mean(mpg),
disp = median(disp)), by = .(get(grouping_vars))]