Search code examples
rdplyrevaltidy

How to use tidy evaluation with column name as strings?


I've read most of the documentation about tidy evaluation and programming with dplyr but cannot get my head around this (simple) problem.

I want to programm with dplyr and give column names as strings as input to the function.

df <- tibble(
  g1 = c(1, 1, 2, 2, 2),
  g2 = c(1, 2, 1, 2, 1),
  a = sample(5),
  b = sample(5)
)

my_summarise <- function(df, group_var) {
  df %>%
    group_by(group_var) %>%
    summarise(a = mean(a))
}

my_summarise(df, 'g1')

This gives me Error : Column 'group_var' is unknown.

What must I change inside the my_summarise function in order to make this work?


Solution

  • We can use also ensym with !!

    my_summarise <- function(df, group_var) {
    
    
      df %>%
        group_by(!!rlang::ensym(group_var)) %>%
        summarise(a = mean(a))
       }
    
    my_summarise(df, 'g1')
    

    Or another option is group_by_at

    my_summarise <- function(df, group_var) {
    
    
          df %>%
            group_by_at(vars(group_var)) %>%
            summarise(a = mean(a))
           }
    
    my_summarise(df, 'g1')