当前位置:  开发笔记 > 编程语言 > 正文

从查找表重命名列

如何解决《从查找表重命名列》经验,为你挑选了1个好方法。

我正在尝试使用查找表中的匹配重命名数据框中的列.

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]
} 

只输出NAs



1> Gregor..:

如果你知道它们的顺序是相同的(就像你的例子中那样)那么你就可以做到

names(mycars) = newvars

如果他们可能不会以相同的顺序(我交换matchdata.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))

推荐阅读
郑谊099_448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有