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.
We can convert to sym
bol 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)))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With