Rename columns in R with the rename() function from dplyr

The rename
function from dplyr can be used to alter column names of a data frame. In addition, rename_with
allows to rename columns using a function.
Syntax
The rename
function alters column names using the syntax new_name = old_name
while rename_with
takes a function (.fn
) to transform the column names from a set of columns (.cols
, all by default).
Rename a single column
For this tutorial we will utilize the band_instruments
dataset from dplyr
, which includes two columns named name
and plays
.
Considering that you want to rename the first column to "First Name"
you can execute the following command:
Notice that you can also rename columns by index. The following example illustrates how to rename the second column of the data set.
Rename multiple columns
It is possible to rename multiple columns at once by adding more new_name = old_name
expressions to the function separated by comma. The following renames the column name
as Member
and the column plays
as Instrument
.
An alternative to the previous is to use a named vector with the old names and new names in conjunction with all_of
or any_of
. The difference is that any_of
won’t throw an error if any “old name” is not on the dataset.
Rename columns using a function with rename_with
The rename_with
function renames all or a set of columns based on a function.
Rename all columns
By default, the rename_with
function will rename all columns based on the input function. In the example below, we use the toupper
function to rename all the columns in uppercase.
If the input function takes more arguments, such as paste or paste0 functions you will need to add ~
before the function and a dot .
to represent the column names. In the following example we also set recycle0 = TRUE
to recycle empty selections if needed.
Note that you can also input custom function as the following, which adds "New_"
as prefix of each column name.
Rename specific columns
By default .cols = everything()
, which means that the input function is applied to all the columns of the data. However, you can select the desired columns by using helper functions such as contains
, starts_with
, ends_with
etc. See the full list of helper functions to select columns.