Rename Columns of a Data Frame in R Programming - rename() Function
Last Updated :
02 May, 2025
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.
R
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:
Using rename() Function2. 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.
R
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:
Using rename() Function3. 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.
R
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:
Using rename() FunctionIn this article, we explored how to rename columns in a data frame in R using the rename()
function from the plyr
package
Similar Reads
Remove names or dimnames from an Object in R Programming - unname() Function unname() function in R Language is used to remove the names or dimnames from an Object. Syntax: unname(x) Parameters: x: Object Example 1: Python3 1== # R Program to remove names from an object # Creating a matrix A = matrix(c(1:9), 3, 3) # Naming rows rownames(A) = c("a", "b",
1 min read
The Factor Issue in a DataFrame in R Programming DataFrames are generic data objects of R which are used to store the tabular data. Data frames are considered to be the most popular data objects in R programming because it is more comfortable to analyze the data in the tabular form. Data frames can also be taught as matrices where each column of a
4 min read
Convert an Object to Data Frame in R Programming - as.data.frame() Function as.data.frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors. Syntax: as.data.frame(object) Parameters:Â object: Vector, Matrix, factor, or data frameR - as.data.frame() Function ExampleExample 1: Basic exam
2 min read
Naming Rows and Columns of a Matrix in R Programming - rownames() and colnames() Function rownames() function in R Language is used to set the names to rows of a matrix. Syntax: rownames(x) <- value Parameters: x: Matrix value: Vector of names to be set Example: Python3 1== # R program to provide a name # to rows of a Matrix # Creating a 3X3 Matrix A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8,
1 min read
Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function data.matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix. Syntax: data.matrix(df) Parameters: df: Data frame to be converted. Example 1: Python3 1== # R program to convert a data frame # into a nu
2 min read
Modify values of a Data Frame in R Language - transform() Function transform() function in R Language is used to modify data. It converts the first argument to the data frame. This function is used to transform/modify the data frame in a quick and easy way. Syntax: transform(data, value) Parameters: data: Data Frame to be modified value: new modified value of data
2 min read