Rename Columns of a Data Frame in R Programming - rename() Function
The
rename()
function in R Programming 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
1. Rename a Data Frame using rename function in R
We are using the plyr
package to rename the columns of a data frame. After creating a data frame with columns row1
, row2
, and row3
, we use the rename()
function to change these column names to one
, two
, and three
. Since rename()
returns a modified copy of the data frame, we assign it back to the original variable, df
, and then print the modified data frame.
install.packages("plyr")
library(plyr)
df<-data.frame(row1 = 0:2, row2 = 3:5, row3 = 6:8)
print("Original Data Frame")
print(df)
print("Modified Data Frame")
rename(df, c("row1"="one", "row2"="two", "row3"="three"))
Output:

2. Modify names of a data frame using rename function
We are using the plyr
package to rename the columns of a data frame. After creating a data frame with columns col1
, col2
, and col3
, we use the rename()
function to change these column names to Name
, Language
, and Age
. The modified data frame is returned with the updated column names.
library(plyr)
df = data.frame(
"col1" = c("abc", "def", "ghi"),
"col2" = c("R", "Python", "Java"),
"col3" = c(22, 25, 45)
)
return(df)
rename(df, c("col1" = "Name", "col2" = "Language", "col3" = "Age"))
Output:

3. Change Multiple Columns using rename function in R
We are using the rename()
function from the plyr
package to rename the columns of the iris
dataset. After loading the iris
data, we change the column names Sepal.Length
, Sepal.Width
, Petal.Length
, and Petal.Width
to iris1
, iris2
, iris3
, and iris4
, respectively. The modified dataset is then returned with the updated column names.
data(iris)
head(iris)
rename_columns<-rename(iris,c('Sepal.Length'='iris1',
'Sepal.Width'='iris2',
'Petal.Length'='iris3',
'Petal.Width'='iris4'))
head(rename_columns)
Output:

In this article, we explored how to rename columns in a data frame in R using the rename()
function from the plyr
package