Rename Columns of a Data Frame in R Programming – rename() Function

rename() function in R Language is used to rename the column names of a data frame, based on the older names.

Syntax: rename(x, names)

Parameters:
x: Data frame
names: Old name and new name

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to rename a Data Frame 
    
# Adding Package 
df <- library(plyr) 
    
# Creating a Data Frame 
df<-data.frame(row1 = 0:2, row2 = 3:5, row3 = 6:8
print("Original Data Frame"
print(df) 
print("Modified Data Frame"
    
# Renaming Data Frame 
rename(df, c("row1"="one", "row2"="two", "row3"="three")) 

chevron_right


Output:

[1] "Original Data Frame"
  row1 row2 row3
1    0    3    6
2    1    4    7
3    2    5    8
[1] "Modified Data Frame"
  one two three
1   0   3     6
2   1   4     7
3   2   5     8

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to modify names of a data frame
  
# Loading library
df <- library(plyr)
  
# Creating a data frame
df = data.frame( 
  "col1" = c("abc", "def", "ghi"), 
  "col2" = c("R", "Python", "Java"), 
  "col3" = c(22, 25, 45
df
  
# Calling rename() function
rename(df, c("col1" = "Name", "col2" = "Language", "col3" = "Age"))

chevron_right


Output:

  col1   col2 col3
1  abc      R   22
2  def Python   25
3  ghi   Java   45
  Name Language Age
1  abc        R  22
2  def   Python  25
3  ghi     Java  45



My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.