How to Rename Columns in R


Often you may want to rename one or more columns in a data frame in R. Fortunately this is easy to do using the rename() function from the dplyr package.

library(dplyr)

This tutorial shows several examples of how to use this function in practice using the following data frame:

#create data frame
df <- data.frame(player = c('a', 'b', 'c', 'd', 'e'),
                 position = c('G', 'F', 'F', 'G', 'G'),
                 points = c(12, 15, 19, 22, 32),
                 rebounds = c(5, 7, 7, 12, 11))

#view data frame
df

  player position points rebounds
1      a        G     12        5
2      b        F     15        7
3      c        F     19        7
4      d        G     22       12
5      e        G     32       11

Example 1: Rename A Single Column

The following code shows how to rename a single column in a data frame:

#rename column 'points' to 'points_scored'
df %>% rename(points_scored = points)

  player position points_scored rebounds
1      a        G            12        5
2      b        F            15        7
3      c        F            19        7
4      d        G            22       12
5      e        G            32       11

Example 2: Rename Multiple Columns

The following code shows how to rename multiple columns at once:

#rename column 'points' and 'rebounds'
df %>% rename(points_scored = points, rebounds_held = rebounds)

  player position points_scored rebounds_held
1      a        G            12             5
2      b        F            15             7
3      c        F            19             7
4      d        G            22            12
5      e        G            32            11

Example 3: Rename All Columns

The following code shows how to use the function rename_all() to perform an operation on the names of all columns in a data frame:

#change all column names to uppercase
df %>% rename_all(toupper)

  PLAYER POSITION POINTS REBOUNDS
1      a        G     12        5
2      b        F     15        7
3      c        F     19        7
4      d        G     22       12
5      e        G     32       11

#change all column names to lowercase
df %>% rename_all(tolower)

  player position points rebounds
1      a        G     12        5
2      b        F     15        7
3      c        F     19        7
4      d        G     22       12
5      e        G     32       11

Example 4: Rename Column By Index

The following code shows how to rename a column by index:

#rename the column in index position 2
df %>% rename(points_scored = colnames(df)[2])

  player points_scored points rebounds
1      a             G     12        5
2      b             F     15        7
3      c             F     19        7
4      d             G     22       12
5      e             G     32       11

Additional Resources

You can find the complete documentation for the rename() function here.

Other useful dplyr functions you may want to understand include functions for removing columns and adding columns.

Leave a Reply

Your email address will not be published. Required fields are marked *