Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R match and replace column names by data frame

Tags:

dataframe

r

I have two data frames. I have a list of column names in a data frame NAME. I then have another data frame DF where the column name is corresponded to data frame NAME in the next column. I need to replace these names to the column names in 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
like image 423
Peter Chung Avatar asked Aug 10 '16 13:08

Peter Chung


People also ask

How would you rename the columns of a data frame with example in R?

colnames () function can be used to change the column names of a data-frame column in R. colnames () function can be used for changing one column name at a time, also all the column names can be changed at once.

How do I replace multiple column names in R?

To change multiple column names by name and by index use rename() function of the dplyr package and to rename by just name use setnames() from data. table . From R base functionality, we have colnames() and names() functions that can be used to rename a data frame column by a single index or name.

How do I change column names in R?

Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.

How do I replace text in a column in R?

Use str_replace() method from stringr package to replace part of a column string with another string in R DataFrame.


2 Answers

How about something like this?

Edit: A better alternative as suggested by @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))
like image 120
Mike H. Avatar answered Oct 07 '22 04:10

Mike H.


There are probably many solutions. I would use plyr::mapvalues

names(DF) <- plyr::mapvalues(names(DF), from = NAME$Name, to = NAME$Real.name)
like image 37
Richard Telford Avatar answered Oct 07 '22 05:10

Richard Telford