I'm trying to rename a variable in a dataframe but can't get the unquoting part to work. I have read http://dplyr.tidyverse.org/articles/programming.html and looking at different examples but unfortunately just can't figure it out somehow. This is what I'm currently using, I want to rename the column "x" to "x2" (both are variables):
df = data.frame(x = c(1,2,3), y = c(2,3,4))
variable_to_rename = "x"
new_variable_name = "x2"
df %>%
rename_vars(names(.), !! variable_to_rename = !! new_variable_name)
The desired output is the dataframe:
data.frame(x2 = c(1,2,3), y = c(2,3,4))
As I just commented: I found the following working code:
df %>%
rename(!!new_variable_name := !!rlang::sym(variable_to_rename))