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.