use function name to name new columns with across()

Is there anyway to use the function name for the glue syntax of .names in across()?

this gives you columns named mean_hp and mean_disp

library(tidyverse)
mtcars %>% summarise(across(c(hp,disp), list(mean=mean), .names="{fn}_{col}"))

it appearse that {fn} is using the name in the list instead of the function name. Id like to have the function be an parameter in rmarkdown but I cant figure how to make a named list out of it. The result of embracing the variable with curlycurly is simply 1_hp and 1_disp because it is unnamed.

centralstat = 'mean'
mtcars %>% summarise(across(c(hp,disp), {{centralstat}}, .names="{fn}_{col}"))

i'm guessing there is either a way to make a named list or some amount of rlang magic to unquote or quote it.

ideally i could do

mtcars %>% summarise(across(c(hp,disp), list({{centralstat}},sd), .names="{fn}_{col}"))

and get e.g. median_hp, median_disp,, sd_hp, sd_disp
any suggestions??

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.