I am trying to use dplyr's new NSE language approach to create a conditional mutate, using a vector input. Where I am having trouble is setting the column equal to itself, see mwe below:
df <- data.frame("Name" = c(rep("A", 3), rep("B", 3), rep("C", 4)),
"X" = runif(1:10),
"Y" = runif(1:10)) %>%
tbl_df() %>%
mutate_if(is.factor, as.character)
ColToChange <- "Name"
ToChangeTo <- "Big"
Now, using the following:
df %>% mutate( !!ColToChange := ifelse(X >= 0.5 & Y >= 0.5, ToChangeTo, !!ColToChange))
Sets the ColToChange
value to Name
, not back to its original value. I am thus trying to use the syntax above to achieve this:
df %>% mutate( !!ColToChange := ifelse(X >= 0.5 & Y >= 0.5, ToChangeTo, Name))
But instead of Name
, have it be the vector.