r - dplyr mutate replace dynamic variable name -


this question has answer here:

the following code replaces values of hwy > 25 1 otherwise 0.

library(ggplot2) data(mpg) mpg %>% mutate(hwybin=replace(hwy>25,1,0)) 

how replace hwy variable name. along lines of:

varname <- "hwy" mpg %>% mutate(hwybin=replace(varname>25,1,0)) 

i feel i'm missing obvious. thank you.

might turn comment answer (and make question reproducible):

library(dplyr) library(ggplot2) library(lazyeval)  data(mpg)  <- mpg %>% mutate(hwybin=replace(hwy>25, 1 ,0))  varname <- "hwy" b <- mpg %>% mutate_(hwybin=interp(~replace(varname>25, 1 ,0),                                    varname=as.name(varname)))  identical(a, b) ## [1] true 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -