Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, dplyr mutate referencing column names by string [duplicate]

Tags:

r

dplyr

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.

like image 428
Canovice Avatar asked Sep 06 '25 03:09

Canovice


1 Answers

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)))))
like image 155
akrun Avatar answered Sep 07 '25 20:09

akrun