4
mydf = data.frame(a = c(1,2,3,4), b = c(5,6,7,8), c = c(3,4,5,6))
var1 = 'a'
var2 = 'b'

mydf = mydf %>% mutate(newCol = var1 + var2)

in our code, var1 and var2 can refer to different columns in mydf, and we need to create newCol by taking the sum of values in the columns whose names are saved in var1 and var2. I understand this can be done outside of dplyr, however I am wondering if there is a solution that uses dplyr and %>% like above.

Canovice
  • 9,012
  • 22
  • 93
  • 211

1 Answers1

5

We can convert to symbol and evaluate with !!

library(dplyr)
mydf %>% 
  mutate(newCol = !! rlang::sym(var1) + !! rlang::sym(var2))

Or another option is subset the column with .data

mydf %>%
   mutate(newCol = .data[[var1]] + .data[[var2]])

or may use rowSums

mydf %>% 
   mutate(newCol = rowSums(select(cur_data(), all_of(c(var1, var2)))))
akrun
  • 874,273
  • 37
  • 540
  • 662