3

Using dplyr it's easy to create a new column as a function of other columns

library(dplyr)
mutate(iris, Sepal.Length + Sepal.Width)

Unfortunately, I have a situation where I need to paste these column names into mutate. I've tried unquoting the strings but that does not work:

mutate(iris, print("Sepal.Length",quote=FALSE) + print("Sepal.Width",quote=FALSE))  

Any suggestions would be greatly appreciated.

zx8754
  • 52,746
  • 12
  • 114
  • 209
iantist
  • 833
  • 2
  • 12
  • 27

1 Answers1

4

Update

This doesn't work anymore. See Ronak's answer below:

How to pass dynamic column names in dplyr into custom function?

Outdated answer

Try below:

mutate_(iris, "Sepal.Length + Sepal.Width")

Notice _ underscore after mutate_

zx8754
  • 52,746
  • 12
  • 114
  • 209