4

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?

dufei
  • 2,166
  • 1
  • 7
  • 18

1 Answers1

4

You need to use across here since you want to apply function for multiple columns.

library(dplyr)
library(rlang)

mean_by <- function(data, by, var) {
   data %>%
     group_by({{ by }}) %>%
     summarise(across({{var}}, mean, .names = '{col}_mean'))
     #For dplyr < 1.0.0 use `summarise_at`
     #summarise_at(vars({{var}}), mean)
}

mtcars %>% mean_by(cyl, c(disp, mpg))
# A tibble: 3 x 3
#    cyl disp_mean mpg_mean
#  <dbl>     <dbl>    <dbl>
#1     4      105.     26.7
#2     6      183.     19.7
#3     8      353.     15.1
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213