Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
How to Rename Multiple Columns in R
Next article icon

How to Rename Multiple Columns in R

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

  1. Using the names function
  2. Using the colNames function
  3. Using "dplyr" package
  4. Using Index
  5. 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.


Next Article
How to Rename Multiple Columns in R

P

pmishra01
Improve
Article Tags :
  • R Language
  • R-DataFrame

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: Python3 # importing module import pyspark from pyspark.sql.functions import col # importing sparksession from pyspark.sql module from pyspark.sql import S
    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: Python3 # importing module import pyspark # importing sparksession from pyspark.sql module from pyspark.sql i
    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: R # load the data.table package library("data.table") # create a data.table with 4 columns # they are id,name,age and address data = data.table(id =
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences