I'm trying to rename the columns in a data frame using matching from a lookup table.
oldvars = c("mpg", "cyl" , "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb")
newvars = c("Miles Per Gallon", "Cycle", "Displacement", "Horsepower", "Distance Rating",
"Working Time", "Quick Second", "Versus", "America", "Gears", "Carbohydrates")
lookup = data.frame(oldvars, newvars)
mycars = mtcars
Using the lookup list to match oldvars and change them into newvars, so that names(mycars)
would output "Miles Per Gallon", "Cycle", "Displacement", "Horsepower", "Distance Rating", "Working Time", "Quick Second", "Versus", "America", "Gears", "Carbohydrates"
I've tried using colnames
to change the names but it isn't reading the variable like I was expecting. The following
for(i in 1:length(newvars)) {
colnames(mycars)[oldvars[i]] = newvars[i]
}
just outputs NA
s