A dataframe in R is a two-dimensional data structure used to store the data in rows and columns and perform different operations on it. In this tutorial, we will look at how to rename one or more column names in an R dataframe with the help of some examples.
How to rename a column in an R dataframe?
You can use the rename()
function available in the dplyr
package to rename one or more column names in a dataframe in R.
The following is the syntax –
rename(dataframe_input, new_column_name=old_column_name)
Pass the dataframe as the first argument and then new_column_name=old_column_name
for each column you want to rename.
It returns an R dataframe with the updated column names.
Steps to rename a column in R using rename()
function
Let’s now look at a step-by-step example of changing the name of a column in a dataframe.
Step 1 – Create a dataframe
First, we will create a dataframe that we will be using throughout this tutorial.
# create a dataframe employees_df = data.frame( "Emp_Name"= c("Jim", "Dwight", "Angela", "Tobi", "Kevin"), "Emp_Age"= c(26, 28, 29, 32, 30), "Emp_Department"= c("Sales", "Sales", "Accounting", "HR", "Accounting") ) # display the dataframe print(employees_df)
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Emp_Name Emp_Age Emp_Department 1 Jim 26 Sales 2 Dwight 28 Sales 3 Angela 29 Accounting 4 Tobi 32 HR 5 Kevin 30 Accounting
We now have a dataframe containing information about some employees working in an office. The dataframe has columns “Emp_Name”, “Emp_Age”, and “Emp_Department”.
You can see that the column names have the prefix “Emp_” which can be removed. We’ll do that by renaming these columns using the rename()
function.
Step 2 – Import the dplyr
package
The rename()
function is defined in the dplyr
package which has to be imported before we can actually use it.
# import dplyr package library("dplyr")
Here we use the library()
function to import the dplyr
package.
Step 3 – Rename column name using rename()
function
Let’s now rename the “Emp_Name” column to “Name” using the rename()
function.
# rename Emp_Name to Name employees_df = rename(employees_df, "Name"="Emp_Name") # display the dataframe print(employees_df)
Output:
Name Emp_Age Emp_Department 1 Jim 26 Sales 2 Dwight 28 Sales 3 Angela 29 Accounting 4 Tobi 32 HR 5 Kevin 30 Accounting
We store the resulting dataframe to the original dataframe variable. You can see that the “Emp_Name” column is now the “Name” column.
Rename multiple columns using the rename()
function
You can also use the rename()
function to rename more than one column at a time.
For example, let’s now rename the “Emp_Age” column to “Age” and the “Emp_Department” column to “Department” in one go.
# rename Emp_Age to Age and Emp_Department to Department employees_df = rename(employees_df, "Age"="Emp_Age", "Department"="Emp_Department") # display the dataframe print(employees_df)
Output:
Name Age Department 1 Jim 26 Sales 2 Dwight 28 Sales 3 Angela 29 Accounting 4 Tobi 32 HR 5 Kevin 30 Accounting
The resulting dataframe has both the column names renamed.
You might also be interested in –
- How to Create a DataFrame in R?
- How to Add a Column to a Dataframe in R?
- Create a Vector in R – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.