A good answer you got by Ritchie. I would add that you can pass the function in a list to fmutate:
airquality |>
fgroup_by(Month) |>
fmutate(across(var_means, list(mean = fmean), .names = TRUE)) |>
fungroup()
you could also use ftransform
with compound pipes and the add_stub
function:
library(magrittr)
airquality %>% ftransform(get_vars(., var_means) %>% fmean(Month, TRA = 1) %>%
add_stub("_mean", pre = FALSE))
If you don't need to rename columns a simple approach would also be to use settransformv
settransformv(airquality, var_means, fmean, Month, TRA = 1, apply = FALSE)
comes very close to what you do with data.table. apply = FALSE
here ensures we use fmean.data.frame
applied to the whole subset of the frame, thus we only need to group once.
A final hybrid option you have is fcomputev
with add_vars<-
or ftransform<-
, where the latter is more intelligent (i.e. it would replace columns if executed again) but the former is faster.
add_vars(airquality) <- airquality |>
fcomputev(var_means, fmean, Month, TRA = 1, apply = FALSE) |>
add_stub("_mean", pre = FALSE)