Search code examples
rdplyrnse

Use I string to refer to a variable inside dplyr?


Suppose I have the following data:

test_df <- data.frame(a=rnorm(100), b=rnorm(100))

The following works:

test_df %>% 
  summarise(y = mean(a))

Now suppose that instead of a i want to pass a character string

string_outcome <- "a" # I want to use this

test_df %>% 
  summarise(y = mean(string_outcome))

That won't work. I tried using !!string_outcome but that does not work either. How can I fix this?


Solution

  • As it is a string, convert it to symbol (sym from rlang) and evaluate (!!)

    test_df %>%
         summarise(y = mean(!! rlang::sym(string_outcome)))
    

    Or use summarise_at which can take strings in vars parameter

    test_df %>%
        summarise_at(vars(string_outcome), list(y = ~  mean(.)))
    

    Or if we need a single value without any attributes, even pull with mean can be used

    test_df %>% 
           pull(string_outcome) %>%
           mean