简体   繁体   English

R 通过数据框匹配和替换列名

[英]R match and replace column names by data frame

I have two data frames.我有两个数据框。 I have a list of column names in a data frame NAME.我在数据框 NAME 中有一个列名列表。 I then have another data frame DF where the column name is corresponded to data frame NAME in the next column.然后我有另一个数据框 DF,其中列名对应于下一列中的数据框 NAME。 I need to replace these names to the column names in DF.我需要将这些名称替换为 DF 中的列名称。

DF:
        A   B   C   D   E
 H001   947 95  10  10  678
 H002   647 40  10  10  806
 H003   840 20  99  53  21
 H004   105 10  97  12  44
 H005   595 59  76  76  67

NAME:
Name Real.name
A    Pete
B    May
C    Jon
D    Paul
E    Emma
F    Fuchs
G    George

Desired output:
        Pete May    Jon Paul Emma
 H001   947  95     10  10   678
 H002   647  40     10  10   806
 H003   840  20     99  53   21
 H004   105  10     97  12   44
 H005   595  59     76  76   67

How about something like this?这样的事情怎么样?

Edit : A better alternative as suggested by @PierreLafortune:编辑:@PierreLafortune 建议的更好的选择:

names(df) <- name$Real.name[match(names(df), name$Name)]

The original approach:原来的做法:

names(df)<-merge(data.frame(Name=names(df)),name,all.x=T)[,"Real.name"]

df
     Pete May Jon Paul Emma
H001  947  95  10   10  678
H002  647  40  10   10  806
H003  840  20  99   53   21
H004  105  10  97   12   44
H005  595  59  76   76   67

Data:数据:

df <- structure(list(A = c(947L, 647L, 840L, 105L, 595L), B = c(95L, 
40L, 20L, 10L, 59L), C = c(10L, 10L, 99L, 97L, 76L), D = c(10L, 
10L, 53L, 12L, 76L), E = c(678L, 806L, 21L, 44L, 67L)), .Names = c("A", 
"B", "C", "D", "E"), class = "data.frame", row.names = c("H001", 
"H002", "H003", "H004", "H005"))

name <- structure(list(Name = structure(1:7, .Label = c("A", "B", "C", 
"D", "E", "F", "G"), class = "factor"), Real.name = structure(c(7L, 
5L, 4L, 6L, 1L, 2L, 3L), .Label = c("Emma", "Fuchs", "George", 
"Jon", "May", "Paul", "Pete"), class = "factor")), .Names = c("Name", 
"Real.name"), class = "data.frame", row.names = c(NA, -7L))

There are probably many solutions.可能有很多解决方案。 I would use plyr::mapvalues我会使用plyr::mapvalues

names(DF) <- plyr::mapvalues(names(DF), from = NAME$Name, to = NAME$Real.name)

This can also be done with dplyr这也可以用dplyr来完成

library(dplyr)

df <- df %>%
      rename_at(as.vector(na.omit(name$Name[match(names(df), name$Name)])), 
               ~as.vector(na.omit(name$Real.name[match(names(df), name$Name)])))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM