Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204771

Rename columns using "relational" dataframe in R

$
0
0

I'm dealing with a large dataframe (over 100 columns) and I need to rename the columns. Let's say the dataframe of interest looks like this:

      C     D     E     F     G     H
1    10   200    50    40    60    10
2    30   400    20    30    30    10
3    20    40    30    30    50    10

I also have a "relational" dataframe with rows matching original column names to the desired new names that looks like this:

  Code  Name 
1 C     Cat  
2 D     Dog  
3 E     Emu  
4 F     Fish 
5 G     Goat 
6 H     Hog  

What I'm looking for is a base or package function that allows me to use this match dataframe to rename the original columns, yielding a final dataframe that looks like this:

    Cat   Dog   Emu  Fish  Goat   Hog
1    10   200    50    40    60    10
2    30   400    20    30    30    10
3    20    40    30    30    50    10

Remember, the real application has something like 100+ columns, so the smallest amount of by hand coding possible is desirable here-- Thanks!


Viewing all articles
Browse latest Browse all 204771

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>