Following the example at https://www.tidyverse.org/blog/2020/02/glue-strings-and-tidy-eval/, I would like to use the function to tunnel multiple data-variables to a summarise
command. The function is defined as:
library(tidyverse)
mean_by <- function(data, by, var) {
data %>%
group_by({{ by }}) %>%
summarise("{{ var }}" := mean({{ var }}, na.rm = TRUE))
}
I would like to get one column for each variable that I pass through var
. However,
mtcars %>%
as_tibble() %>%
mean_by(cyl, c(disp, mpg))
only yields a single column, apparently by attaching the vectors disp
and mpg
to each other.
Is there any other way to account for multiple variables using the glue syntax?