In this article, we’d like to show you how to rename data frame columns in R by using R base functions or other libraries such as dplyr, plyr, which is often needed when manipulating our data.

1. Preparation

We will prepare a data frame so that we can practice renaming its columns in the below sections. Here is the code to create a simple data frame of books.

Let’s see the data frame

1. Rename column of data frame with R base functions

In this sections, we will rename column of a data frame by using R base functions which don’t require us to install any other libraries or packages.

1.1. Use the colnames() function

The colnames function is used to retrieve or set the column names.

1.1.1. Change or rename all columns

1.1.2. Change or rename a column

We can rename the column by specifying the index of the column we want to change in the column names vector.

or specify exactly the column name we want to change or rename:

or

1.2. Use the names() function

We can use the names() function which is similar to the colnames() function, to rename column of a data frame.

1.2.1. Rename all columns

1.1.2. Rename a specific column

We can change the column name by specifying the index of the column we want to rename in the column names vector.

or specify exactly the column name we want to rename:

or

2. Rename a data frame column with the dplyr package

Another R package enables us to rename the column of data frame in R is the dplyr package. Let’s get started by install and load the package

2.1. Install and load the dplyr package

To use the package, we need to load it by the below command:

2.2. Use the rename() function to change or rename the column

For example, to change the “bookNames” column to “Title”

And to change the columns “bookNames” to “Title and “bys” to “Author”

2.3. Use the select() function

We can use the select function to rename the column of a data frame. Just note that this function only keeps only variables we specify in the arguments.

For example, we will change the columns “bookNames” to “Title and “bys” to “Author”.

Note that the newdf data frame only remains 2 columns Title and Author.

3. Rename column of data frame with the plyr package

In similar to the dplyr, we can use the plyr package to change the column names of a data frame in R.

3.1. Install and load the plyr package

To use the package, we need to load it by the below command:

3.2. Use the rename() function to rename the columns

For example, we will change the names of columns “bookNames” to “Title and “bys” to “Author” by using the rename function:

or

4. Summary

We have seen several ways to rename data frame columns in R. The first way is to use the base R functions which doesn’t require us to install any additional packages. The second way is to use other R packages such as plyr, dplyr, etc. These packages provide us a shorter way to rename data frame columns.

Below are other posts related to R data frame. You can refer to them if you’re interested in.

R – Add New Column To A Data Frame

R – How To Order A Data Frame

R Data Frame and basic functions

R – Add New Column To A Data Frame

0 0 vote
Article Rating