I am trying to write a function that (partially) renames a variable by combining its original data frame and the existing variable name. In essence, I want:
df1 <- data.frame(a = 1, b = 2)
to become:
df1 %>% rename(df1_a = a)
But I want to do it programmatically, something like:
fun <- function(df, var) { outdf <- rename_(df, paste(df, var, sep = "_") = var) return(outdf) }
This admittedly naive approach obviously does not work, but I could not understand it. I'm sure the answer is somewhere in the nse vignette ( https://cran.r-project.org/web/packages/dplyr/vignettes/nse.html ), but that does not seem to affect the construction of variable names.
source share