Sometimes everything is in order not to do everything in dplyr . This may be one of those occasions. I would set a vector that works like a key:
namekey <- c(mpg="miles_per_gallon", cyl="cylinders", disp="displacement", hp="horse_power", drat="rear_axle_ratio", wt="weight", qsec="quarter_mile_time", vs="v_s", am="transmission", gear="number_of_gears", carb="number_of_carburetors", foo="missing_variable") mtcars1 <- mtcars[,1:2] mtcars1$foo <- rnorm(nrow(mtcars1)) names(mtcars1) <- namekey[names(mtcars1)] head(mtcars1)
Once you have the key, this is just one, easy-to-understand line of code that renames.
source share