我正在尝试使用查找表中的匹配重命名数据框中的列.
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
使用查找列表匹配oldvars并将其更改为newvars,以便names(mycars)
输出"Miles Per Gallon", "Cycle", "Displacement", "Horsepower", "Distance Rating", "Working Time", "Quick Second", "Versus", "America", "Gears", "Carbohydrates"
我已经尝试过colnames
更改名称,但它并没有像我期望的那样读取变量.下列
for(i in 1:length(newvars)) { colnames(mycars)[oldvars[i]] = newvars[i] }
只输出NA
s
如果你知道它们的顺序是相同的(就像你的例子中那样)那么你就可以做到
names(mycars) = newvars
如果他们可能不会以相同的顺序(我交换match
和data.table
):
oldvars = c("cyl" ,"mpg", "disp", "foo") newvars = c( "Cycle", "Miles Per Gallon", "Displacement", "bar") mycars = head(mtcars, 2) # short data frame to work with
然后setnames
是你的朋友确定正确的顺序:
name_match = match(names(mycars), lookup$oldvars) name_match [1] 2 1 3 NA NA NA NA NA NA NA NA # assign the names names(mycars)[na.omit(name_match)] = newvars[!is.na(name_match)]
所以可以完成任务
library(data.table) setDT(mycars) # convert the data frame to data.table setnames(mycars, old = oldvars, new = newvars) # update names by reference
如果你不喜欢这个plyr
范例,那么rename
也可以:
plyr::rename(mycars, setNames(newvars, oldvars))