The following code replaces the values of hwy > 25 with 1 otherwise 0.
library(ggplot2)
data(mpg)
mpg %>% mutate(hwybin=replace(hwy>25,1,0))
How would I do the replace with hwy as a variable name. Something along the lines of:
varname <- "hwy"
mpg %>% mutate(hwybin=replace(varname>25,1,0))
I feel like I'm missing something obvious. Thank you.
hwybin
dynamically? Or substitutevarname
dynamically? – Rich Scrivenmpg %>% mutate_(hwybin=interp(~replace(varname>25, 1 ,0), varname = as.name(varname)))
ought to do it – hrbrmstr