r - dplyr mutate replace dynamic variable name -
this question has answer here:
- dplyr string column reference 2 answers
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
Post a Comment