In this article we will learn how to rename columns in a dataframe in R using rename() command from dplyr package.


Theory

Did you ever come across a problem when you import a dataset and you wanted to rename of a column?

There could be different reasons for it. Sometimes for aesthetics purposes (like changing it from “nAmE” to “Name”) or if the column had a confusing name and you wanted to make it more useful (like changing from “fn” to “first_name”).

This problem arises quite often as you work with more and more datasets.

Therefore, let’s develop a skill that will allow you to address this issue quickly and efficiently.

It’s time to learn how to rename columns in R!

Below I will show an example of of the usage of the rename() command from dplyr package.



Application

Below are the steps we are going to take to make sure we do master the skill of renaming columns in R:

  1. Installing dplyr package
  2. Basic rename() command description
  3. Loading sample dataset: economics
  4. How to rename column in R



Part 1. Installing dplyr package

R does have commands built in that can solve a similar program, yet rename() command from dplyr package is more user friendly and easier to follow (in my opinion).

You can learn more about dplyr package here.

Therefore, let’s proceed to installing the package.

In order to install and “call” the package into your environment, you should use the following code:


install.packages("dplyr")
library(dplyr)



Part 2. Basic rename() command description

The short theoretical explanation of the function is the following:

rename(data, new_name=old_name)

Here, “data” refers to your dataset (in my case I will call it “mydata”);
“new_name” is the new name that you would like to have for the column;
“old_name” is the old (original) name of the column in the dataset.



Part 3. Loading sample dataset: economics

R has a variety datasets already built into it. Although the step of “loading” this dataset isn’t required, it’s a good practice to get familiar with 🙂

I prefer to call the data I work with “mydata”, so here is the command you would use for that:


mydata<-economics

This built-in dataset is the US economic time series.

Take a look at the dataset and the variables it contains:


View(mydata)

The variable we will be plotting in this tutorial is "unemploy", which is the number of unemployed (in thousands).

Note: you can import your own dataset into R, and follow the same procedures in this article.



Part 4. How to rename column in R

Our goal now is to rename the column in the existing dataset.

I would like to replace the original column name "unemploy" with a new column name "unemployment".

At this point we are all set to rename the column in R!

You can do it using the following code:


mydata<-rename(mydata, unemployment=unemploy)

Amazing! It's completed!

You can take a look at the dataset again, and will see that the name of the column changed.


View(mydata)

Now you have mastered the procedure and now can rename column in R!



If you are interested to learn more about Data Manipulation, there are also plenty of articles covering different types of data merging.