How to match and replace column names stored in R data frames in R-Programming?



If we have a data frame that contains a column of column names which matches with the column names of a data frame and another column that has different values then we can set these different values as the new column names of the data frame having matched column names.

This can be done with the help of match function. Check out the below Examples to understand how it works.

Example 1

Following snippet creates a sample data frame −

x1<-rnorm(20) x2<-rnorm(20) x3<-rnorm(20) df1<-data.frame(x1,x2,x3) df1

The following dataframe is created

 x1 x2 x3 1 0.76231437 -0.18459872 -0.836848620 2 0.31998649 1.24599469 0.096976964 3 0.11319510 -1.19200362 -0.824255747 4 0.47570366 0.24791478 0.355295166 5 -0.84546806 1.05928526 -1.073653485 6 -1.89667914 0.92187888 1.552141225 7 -1.81604483 0.05974304 -0.244209195 8  0.05762202 0.14238099 0.957877205 9 -0.79539907 0.42396450 0.219413291 10 0.47248362 2.44519391 -0.126831024 11 0.91802738 -0.84282349 1.577785667 12 -1.30378165 1.04226653 -0.528164742 13 0.88999575 1.74773475 1.409087713 14 -0.43192360 0.11203207 0.976614195 15 -0.68683934 0.67849795 -0.004272223 16 -0.68894471 1.33377663 -0.397701119 17 1.06729808 -0.04649078 0.578908343 18 -0.73322670 -0.52255298 0.355650527 19 0.28593460 -1.19460885 -0.094308012 20 0.76086277 -0.38646442 -0.484786025

Add the following code to the above snippet −

x1<-rnorm(20) x2<-rnorm(20) x3<-rnorm(20) df1<-data.frame(x1,x2,x3) df1_col<-data.frame(Var1=c("x","y","z"),Var2=c("x1","x2","x3")) df1_col

If you execute all the above given snippets as a single program, it generates the following Output −

 Var1 Var2 1 x x1 2 y x2 3 z x3

To change the column names of df1 to values in Var1 in df1_col on the above created data frame, add the following code to the above snippet −

x1<-rnorm(20) x2<-rnorm(20) x3<-rnorm(20) df1<-data.frame(x1,x2,x3) df1_col<-data.frame(Var1=c("x","y","z"),Var2=c("x1","x2","x3")) names(df1) <- df1_col$Var1[match(names(df1),df1_col$Var2)] df1 

Output

If you execute all the above given snippets as a single program, it generates the following Output −

 x y z 1 0.76231437 -0.18459872 -0.836848620 2 0.31998649 1.24599469 0.096976964 3 0.11319510 -1.19200362 -0.824255747 4 0.47570366 0.24791478 0.355295166 5 -0.84546806 1.05928526 -1.073653485 6 -1.89667914 0.92187888 1.552141225 7 -1.81604483 0.05974304 -0.244209195 8 0.05762202 0.14238099 0.957877205 9 -0.79539907 0.42396450 0.219413291 10 0.47248362 2.44519391 -0.126831024 11 0.91802738 -0.84282349 1.577785667 12 -1.30378165 1.04226653 -0.528164742 13 0.88999575 1.74773475 1.409087713 14 -0.43192360 0.11203207 0.976614195 15 -0.68683934 0.67849795 -0.004272223 16 -0.68894471 1.33377663 -0.397701119 17 1.06729808 -0.04649078 0.578908343 18 -0.73322670 -0.52255298 0.355650527 19 0.28593460 -1.19460885 -0.094308012 20 0.76086277 -0.38646442 -0.484786025

Example 2

Following snippet creates a sample data frame −

a<-rpois(20,2) b<-rpois(20,5) c<-rpois(20,2) df2<-data.frame(a,b,c) df2 

The following dataframe is created

 a b c 1 3 6 0 2 1 7 5 3 2 5 3 4 1 1 2 5 4 9 2 6 0 3 3 7 1 6 1 8 0 6 3 9 4 6 2 10 1 4 2 11 3 2 2 12 0 6 2 13 1 4 2 14 0 7 2 15 4 2 2 16 2 4 5 17 2 3 0 18 4 5 4 19 3 3 1 20 3 4 2

Add the following code to the above snippet −

a<-rpois(20,2) b<-rpois(20,5) c<-rpois(20,2) df2<-data.frame(a,b,c) df2_col<-data.frame(Ranks=c("Rank1","Rank2","Rank3"),Var=c("a","b","c")) df2_col 

Output

If you execute all the above given snippets as a single program, it generates the following Output −

 Ranks Var 1 Rank1 a 2 Rank2 b 3 Rank3 c

To change the column names of df2 to values in Var in df2_col on the above created data frame, add the following code to the above snippet −

a<-rpois(20,2) b<-rpois(20,5) c<-rpois(20,2) df2<-data.frame(a,b,c) df2_col<-data.frame(Ranks=c("Rank1","Rank2","Rank3"),Var=c("a","b","c")) names(df2)<-df2_col$Ranks[match(names(df2),df2_col$Var)] df2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

 Rank1 Rank2 Rank3 1    3     6    0 2    1     7 5 3    2     5 3 4    1     1 2 5    4   9 2 6    0 3 3 7    1 6 1 8    0 6 3 9    4 6 2 10   1 4 2 11   3 2 2 12   0 6 2 13   1 4 2 14   0 7 2 15   4 2 2 16   2 4 5 17   2 3 0 18   4 5 4 19   3 3 1 20   3 4 2
Updated on: 2021-11-01T05:57:48+05:30

932 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements