I am trying to calculate grouped means using collapse package. Below is an example of what I am trying to achieve.
library(data.table)
library(collapse)
data_1 <- as.data.table(airquality)
var_means <- c(
"Ozone",
"Solar.R",
"Wind"
)
data_1[,paste0(var_means,"_mean") := lapply(.SD,mean,na.rm = TRUE),by = .(Month)]
There are at least a couple of ways. Using the
dplyr-style syntax:Or using
ftransform():Or if you want to pass a character vector of columns you need something like: