This likely a duplicate somewhere on stackoverflow and is a followup question to dplyr mutate replace dynamic variable name.
Now I am trying to use a variable name as th
Rather than replace mpg
's existing hwy
variable I did the following to illustrate the same thing and put it into context:
From your previous question:
library(dplyr)
library(ggplot2)
library(lazyeval)
data(mpg)
# "normal" dplyr
a <- mpg %>% mutate(hwybin=replace(hwy>25,1,0))
# varying just the RHS
varname <- "hwy"
b <- mpg %>% mutate_(hwybin=interp(~replace(varname>25, 1 ,0),
varname=as.name(varname)))
identical(a, b)
For this particular question:
# varying both RHS and LHS
colname <- "hwybin"
z <- mpg %>% mutate_(.dots=setNames(list(interp(~replace(varname>25, 1 ,0),
varname=as.name(varname))), colname))
# tests
identical(a, z)
identical(b, z)