How can I use variables in place of column names in dplyr strings? As an example say I want to add a column to the iris dataset called sum that is the sum of Sepal.Length and Sepal.Width. In short I want a working version of the below code.
x = "Sepal.Length"
y = "Sepal.Width"
head(iris%>% mutate(sum = x+y))
Currently, running the code outputs "Evaluation error: non-numeric argument to binary operator" as R evaluates x and y as character vectors. How do I instead get R to evaluate x and y as column names of the dataframe? I know that the answer is to use some form of lazy evaluation, but I'm having trouble figuring out exactly how to configure it.
Note that the proposed duplicate: dplyr - mutate: use dynamic variable names does not address this issue. The duplicate answers this question:
Not my question: How do I do:
var = "sum"
head(iris %>% mutate(var = Sepal.Length + Sepal.Width))
dplyr
function, use!! as.name(x)
. Herehead(iris%>% mutate(sum = !!as.name(x) + !!as.name(y)))
– De NovoError in !as.name(y) : invalid argument type
.So it seems like R isn't correctly evaluating+ !!as.name(y)
– Ajjit NarayananpackageVersion("dplyr")
. You need0.7
or higher. – De Novohead(iris%>% mutate(sum = !!as.name(x)))
everything works fine and a new column named sum is returned (with values equal to Sepal.Length). But when i add+!!as.name(y)
to the command (ie run your command) I get the above error. So R seems to specifically having a problem processing the second set of!!
. Does it work locally on your computer? If so, it might just be a problem with my R session – Ajjit Narayananx <- "Sepal.Length"; y <- "Sepal.Width"
. – De Novo