I am trying to name the output of an across
function using both {col}
and one of the function arguments in
library(tidyverse)
mean_by <- function(data, by, var, prefix = "avg") {
data %>%
group_by({{ by }}) %>%
summarise(across({{ var }}, mean, .names = "{prefix}_{col}"))
}
However, when testing on iris %>% mean_by(Species, Sepal.Width)
, I get the error that object 'prefix' cannot be found. The glue syntax in .names
looks okay to me so I assume this is a scoping issue? Can the .names
argument only access what has been passed to across
?