Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste in column names into dplyr::mutate [duplicate]

Tags:

r

dplyr

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.

like image 858
iantist Avatar asked Dec 08 '25 07:12

iantist


1 Answers

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_

like image 183
zx8754 Avatar answered Dec 09 '25 19:12

zx8754



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!