rename column in an R dataframe

Rename Column Name in R Dataframe

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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Authors

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

  • Gottumukkala Sravan Kumar
Scroll to Top