3 Ways to Rename (Multiple) Columns in R

This article demonstrates 3 easy ways to rename one or more columns in R.

By default, all tables, data frames, and tibbles in R have column names (i.e., variable names). However, these names do not always have the text you want. For example, the text can have a default value after importing data or creating a new column. So, how do you change column names?

The easiest way to rename columns in R is by using the setnames() function from the “data.table” package. This function modifies the column names given a set of old names and a set of new names. Alternatively, you can also use the colnames() function or the “dplyr” package.

In this article, we will discuss the 3 different methods and show how to use them. For illustrative purposes, we will use the data frame shown below where we will change the columns “a” and “b” into “a_new” and “b_new”, respectively.

An R dataframe

Each method contains an R code snippet that you can use directly in your own projects.

1. Rename Columns with the colnames() Function

The first method to modify column names in R is by using the colnames() function.

The colnames() function is a standard base R function that you can use without installing additional packages. However, this function was originally designed to set the names of columns, not necessarily rename columns. Nevertheless, you can still use the colnames() function to modify column names.

These are the steps to rename columns in R using the colnames() function:

  1. Start the colnames() function and specify the data frame in which you want to change the column names. For example, colnames(my_data).
  2. Use the assignment operator (i.e., <-) to start the assignment.
  3. Provide the new column names in a vector.

The vector with the new columns must meet some requirements, namely:

  • The new column names are written between (double) quotes and separated by a comma. For example, c(“a_new”, “b_new”).
  • The length of the vector, i.e., the number of names, must be equal to the number of the columns in your data frame.

Although this method is straightforward, it has one great drawback. Irrespectively of the number of columns you want to rename, you must always specify all column names (i.e., the second requirement). Even those that you don’t want to change.

Moreover, if you don’t specify all column names, R assigns a default value to those columns that you didn’t specify.

For instance, in the code below we want to change only the first 2 (out of 5) column names. Therefore, we define the new column names “a_new” and “b_new“, but also the other (unchanged) names.

my_df <- data.frame(a = c(1:5),
                    b = letters[1:5],
                    c = seq(10,50,10),
                    d = LETTERS[22:26],
                    e = c("@", "#", "!", "&", "%"))
my_df

colnames(my_df) <- c("a_new", "b_new", "c", "d", "e")
my_df
Rename column names in R with the colnames() function

Nevertheless, you can avoid this problem by specifying which columns you want to change. However, this is done by the position of the columns. Hence, it requires that you know the exact position of each column name in the data frame.

In the example below, we use the vector c(1,2) to explicitly specify that we want to change the first 2 column names.

my_df <- data.frame(a = c(1:5),
                    b = letters[1:5],
                    c = seq(10,50,10),
                    d = LETTERS[22:26],
                    e = c("@", "#", "!", "&", "%"))
my_df

colnames(my_df)[c(1,2)] <- c("a_new", "b_new")
my_df
Change variable names in R with the colnames() function

Instead of using the colnames() function, you can also use the names() function. Both functions work exactly the same and use the same syntax.

2. Rename Columns with the setnames() Function

The second method to rename columns in R uses the setnames() function.

The setnames() function is part of the data.table package and is (one of) the most convenient functions to modify column names. It is both intuitive, and fast, and can be used in combination with dplyr.

To rename columns of a data frame with the setnames() function you need to provide 3 arguments, namely:

  1. The name of your data frame.
  2. A vector with the names of the columns you want to change.
  3. A vector with the new column names.

Like the previous method, the column names (both new and old) must be written between (double) quotes and must be separated by commas.

It is important to mention that the setnames() function is case-sensitive. That means that it treats “A” and “a” differently. If you don’t know whether the names are written in upper or lower case, you might want to change the case of the columns before renaming them.

The code snippet below demonstrates how to use the setnames() function.

library(data.table)

my_df <- data.frame(a = c(1:5),
                    b = letters[1:5],
                    c = seq(10,50,10),
                    d = LETTERS[22:26],
                    e = c("@", "#", "!", "&", "%"))
my_df

old_names <- c("a", "b")
new_names <- c("a_new", "b_new")

setnames(my_df, old = old_names, new = new_names)
my_df
Rename variables with the setnames() function

Note that the R programming language also has the setNames() function (with a capital “N”). This function does not change column names. Instead, you can use it to set the names of the columns.

3. Rename Columns with the dplyr Package

The third method to rename columns in R is by using the dplyr package.

The dplyr package provides many useful functions and operations for data processing. Because of its versatility, the dplyr package is one of the most used packages in R. As a result, there are always many ways to solve a problem. For example, there are at least 3 ways to rename columns with dplyr.

Remember, before you can use (some of) the function mentioned below, you need to install and load the dplyr package. You can do this with the following code.

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

Rename columns with dplyr using the rename() function

First, you can use the rename() function. This function is used frequently because of its simplicity. First, you define the new name, then an equal sign, and finally the old name. However, if you want to rename multiple columns, you must specify each column separately, which is a disadvantage.

For example:

library(dplyr)

my_df <- data.frame(a = c(1:5),
                    b = letters[1:5],
                    c = seq(10,50,10),
                    d = LETTERS[22:26],
                    e = c("@", "#", "!", "&", "%"))
my_df

my_df %>% 
  rename(a_new = a, b_new = b)
Rename column names with dplyr

Note that, in contrast to all other methods, the rename() function expects first the new column name and then the old column name. Whereas all other functions start with the old name followed by the new name.

Rename columns with dplyr using the rename_at() function

Secondly, you can use the rename_at() function to change column names with dplyr.

This function requires two arguments. Namely, a vector with the old column names and a vector with the new column names. First, the vars() function selects the columns mentioned in the prior vector. Then, it assigns the names mentioned in the latter one.

Compared to the rename() function, the rename_at() function has the advantage that you can use vectors to modify column names.

For instance:

library(dplyr)

my_df <- data.frame(a = c(1:5),
                    b = letters[1:5],
                    c = seq(10,50,10),
                    d = LETTERS[22:26],
                    e = c("@", "#", "!", "&", "%"))
my_df

old_names <- c("a", "b")
new_names <- c("a_new", "b_new")

my_df %>% 
  rename_at(vars(old_names), ~new_names)
Use dplyr to change columns in R

Rename columns with dplyr using the setnames() function

Lastly, you can use the setnames() function in combination with dplyr to rename columns in R.

The way the setnames() function works with dplyr is identical to what we’ve explained before. Hence, you specify the old variable names and the names of the new variables (using two vectors), and the setnames() function will do the rest.

Below we provide an example.

library(dplyr)

my_df <- data.frame(a = c(1:5),
                    b = letters[1:5],
                    c = seq(10,50,10),
                    d = LETTERS[22:26],
                    e = c("@", "#", "!", "&", "%"))
my_df

old_names <- c("a", "b")
new_names <- c("a_new", "b_new")

my_df %>% 
  setnames(old_names, new_names)
my_df
Rename variables in R