Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

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

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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:




# 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")) 

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:




# 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"))

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
Last Updated : 19 Jun, 2020
Like Article
Save Article
Similar Reads