2015-11-14 38 views
0

我与默认列名V1,V2,V3,V230,等等的数据帧重命名数据帧列中的R使用查找数据帧

我有含有具有2列另一个数据帧中,一个V1,V2,V3等,第二列包含一个字符串。

我想重新使用第二个数据帧作为查找表在forst数据帧中的列。

请注意,第一个数据帧的列数少于第二个“查找”数据帧中列出的数量。

任何想法?

回答

0

我们可以使用match

colnames(firstdat) <- seconddat[,2][match(colnames(firstdat), 
         seconddat[,1], nomatch=0)] 
0

说第一数据帧是x和第二个是y

colnames(x) <- merge(data.frame(colnames(x)), y, by.x ="colnames.x.", by.y= "Col1")[,2] 

### Col1 is the name of column 1 of *y* (containing V1, V2 etc) 
0

考虑使用data.table

library(data.table) 
DT <- as.data.table(df) 
temp.lookup <- lookup[lookup$oldnames %in% names(DT), ] 
setnames(DT, old = temp.lookup$oldnames, new = temp.lookup$newnames)