How to Rename Multiple Columns in R
Last Updated :
24 Apr, 2025
Renaming columns in R Programming Language is a basic task when working with data frames, and it's done to make things clearer. Whether you want names to be more understandable, follow certain rules, or match your analysis, there are different ways to change column names.
There are types of methods available for Rename Multiple Columns in R
- Using the names function
- Using the colNames function
- Using "dplyr" package
- Using Index
- Using Setnames
R
# Create a sample data frame
df <- data.frame(
old_name1 = c(1, 2, 3),
old_name2 = c(4, 5, 6),
old_name3 = c(7, 8, 9)
)
# Display the original data frame
print("Original Data Frame:")
print(df)
Output:
[1] "Original Data Frame:" old_name1 old_name2 old_name3
1 1 4 7
2 2 5 8
3 3 6 9
Use the names() function to rename the columns
R
# Specify the new column names
new_names <- c("new_name1", "new_name2", "new_name3")
# Rename columns using names()
names(df) <- new_names
# Display the data frame with renamed columns
print("Data Frame with Renamed Columns:")
print(df)
Output:
[1] "Data Frame with Renamed Columns:" new_name1 new_name2 new_name3
1 1 4 7
2 2 5 8
3 3 6 9
Using colNames() function
R
# Create a sample data frame
df <- data.frame(
old_Frame1 = c(1, 2, 3),
old_Frame2 = c(4, 5, 6),
old_Frame3 = c(7, 8, 9)
)
# Display the original data frame
print("Original Data Frame:")
print(df)
Output
[1] "Original Data Frame:" old_Frame1 old_Frame2 old_Frame3
1 1 4 7
2 2 5 8
3 3 6 9
Now , use the colnames() function for renaming the multiple columns
R
# Specify the new column names
new_names <- c("new_Frame1", "new_Frame2", "new_Frame3")
# Rename columns using colnames()
colnames(df) <- new_names
# Display the data frame with renamed columns
print("Data Frame with Renamed Columns:")
print(df)
Output:
[1] "Data Frame with Renamed Columns:" new_Frame1 new_Frame2 new_Frame3
1 1 4 7
2 2 5 8
3 3 6 9
Rename Multiple Columns in R Using "dplyr" package
R
# Load the dplyr package
library(dplyr)
# Create a sample data frame
df <- data.frame(
old_Column1 = c(1, 1, 1),
old_Column2 = c(5, 5, 5),
old_Column3 = c(8, 8, 8)
)
# Display the original data frame
print("Original Data Frame:")
print(df)
Output:
[1] "Original Data Frame:" old_Column1 old_Column2 old_Column3
1 1 5 8
2 1 5 8
3 1 5 8
Use the dplyr package rename function for renaming multiple columns
R
# Rename columns using dplyr's rename() function
df <- df %>%
rename(new_Column1 = old_Column1,
new_Column2 = old_Column2,
new_Column3 = old_Column3)
# Display the data frame with renamed columns
print("Data Frame with Renamed Columns:")
print(df)
Output:
[1] "Data Frame with Renamed Columns:" new_Column1 new_Column2 new_Column3
1 1 5 8
2 1 5 8
3 1 5 8
Rename Multiple Columns in R Using Index
R
# Rename multiple columns by index
# Load library
library(dplyr)
# Create a sample data frame
my_dataframe <- data.frame(
old1 = c(1, 2, 3),
old2 = c(4, 5, 6),
old3 = c(7, 8, 9)
)
print("Old Dataframe")
print(my_dataframe)
Output
[1] "Old Dataframe" old1 old2 old3
1 1 4 7
2 2 5 8
3 3 6 9
Use the Column Index for renaming
R
# Rename columns by index
my_dataframe <- my_dataframe %>%
rename(c1 = !!1, c2 = !!2)
# Print the data frame with renamed columns
print("New Dataframe")
print(my_dataframe)
Output:
[1] "New Dataframe" c1 c2 old3
1 1 4 7
2 2 5 8
3 3 6 9
Rename Multiple Columns in R Using Setnames ()
R
# Load the data.table package
library(data.table)
# Create a sample data.table
my_data_table <- data.table(
oldName1 = c(1, 2, 3),
oldName2 = c(4, 5, 6),
oldName3 = c(7, 8, 9)
)
print("Orginal Dataframe")
print(my_data_table)
Output:
[1] "Orginal Dataframe" oldName1 oldName2 oldName3
1: 1 4 7
2: 2 5 8
3: 3 6 9
Use the data.table package's setnames() function for renaming the columns
R
# Specify the new column names
new_names <- c("newName1", "newName2", "newName3")
# Rename columns using setnames()
setnames(my_data_table, old = c("oldName1", "oldName2", "oldName3"), new = new_names)
# Print the data.table with renamed columns
print("New Dataframe")
print(my_data_table)
Output:
[1] "New Dataframe" newName1 newName2 newName3
1: 1 4 7
2: 2 5 8
3: 3 6 9
Conclusion
In R, changing column names is easy and can be done in different ways. We can use basic functions like names() and colnames(), the helpful dplyr package with its rename() function, specify column positions, or use setnames() from the data.table package. Each approach caters to different preferences and scenarios, providing users with flexibility in adapting their code to suit diverse data manipulation requirements.
Similar Reads
How to rename multiple columns in PySpark dataframe ?
In this article, we are going to see how to rename multiple columns in PySpark Dataframe. Before starting let's create a dataframe using pyspark: [GFGTABS] Python3 # importing module import pyspark from pyspark.sql.functions import col # importing sparksession from pyspark.sql module from pyspark.sq
2 min read
How to Rename a Column in PL/SQL?
Renaming a column in PL/SQL is a fundamental operation in Oracle Database management. It enhances clarity, maintains consistency, or accommodates evolving data requirements. Database administrators can ensure the data integrity and process of streamlining data manipulation by altering the column nam
4 min read
How to Rename Multiple PySpark DataFrame Columns
In this article, we will discuss how to rename the multiple columns in PySpark Dataframe. For this we will use withColumnRenamed() and toDF() functions. Creating Dataframe for demonstration: [GFGTABS] Python3 # importing module import pyspark # importing sparksession from pyspark.sql module from pys
2 min read
Remove Multiple Columns from data.table in R
In this article, we are going to see how to remove multiple columns from data.table in the R Programming language. Create data.table for demonstration: [GFGTABS] R # load the data.table package library("data.table") # create a data.table with 4 columns # they are id,name,age and address da
2 min read
How To Remove A Column In R
R is a versatile language that is widely used in data analysis and statistical computing. A common task when working with data is removing one or more columns from a data frame. This guide will show you various methods to remove columns in R Programming Language using different approaches and provid
4 min read
How to Rename Factor Levels in R?
In this article, we are going to how to rename factor levels in R programming language. A factor variable in R is represented using categorical variables which are represented using various levels. Each unique value is represented using a unique level value. A factor variable or vector in R can be d
4 min read
Add Multiple New Columns to data.table in R
In this article, we will discuss how to Add Multiple New Columns to the data.table in R Programming Language. To do this we will first install the data.table library and then load that library. Syntax: install.packages("data.table") After installing the required packages out next step is to create t
3 min read
How to Aggregate multiple columns in Data.table in R ?
In this article, we will discuss how to aggregate multiple columns in Data.table in R Programming Language. A data.table contains elements that may be either duplicate or unique. As a result of this, the variables are divided into categories depending on the sets in which they can be segregated. The
5 min read
How to Rename Columns in Tidyverse
Renaming columns is an important step in data processing since it allows for easier interpretation and analysis. Within the field of data research, the Tidyverse package provides extensive capabilities for this goal, including quick ways for renaming columns smoothly. What is Tidyverse?Tidyverse is
3 min read
How to add multiple columns to a data.frame in R?
In R Language adding multiple columns to a data.frame can be done in several ways. Below, we will explore different methods to accomplish this, using some practical examples. We will use the base R approach, as well as the dplyr package from the tidyverse collection of packages. Understanding Data F
4 min read