How to Rename a Single Column in R?

To rename a single column in R, you can use the colnames() function. This function takes a vector of the old and new column names as arguments and renames the column accordingly. You can also use the rename() or rename_all() functions from the dplyr package for more complex renaming tasks.


You can use one of the following methods to rename a single column in a data frame in R:

Method 1: Rename a Single Column Using Base R

#rename column by name
colnames(df)[colnames(df) == 'old_name'] <- 'new_name'

#rename column by position
#colnames(df)[2] <- 'new_name'

Method 2: Rename a Single Column Using dplyr

library(dplyr)

#rename column by name
df <- df %>% rename_at('old_name', ~'new_name')

#rename column by position
df <- df %>% rename_at(2, ~'new_name')

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1    A     99      33       30
2    B     90      28       28
3    C     86      31       24
4    D     88      39       24
5    E     95      34       28

Example 1: Rename a Single Column Using Base R

The following code shows how to rename the points column to total_points by using column names:

#rename 'points' column to 'total_points'
colnames(df)[colnames(df) == 'points'] <- 'total_points'

#view updated data frame
df

  team total_points assists rebounds
1    A           99      33       30
2    B           90      28       28
3    C           86      31       24
4    D           88      39       24
5    E           95      34       28

The following code shows how to rename the points column to total_points by using column position:

#rename column in position 2 to 'total_points'
colnames(df)[2] <- 'total_points'

#view updated data frame
df

  team total_points assists rebounds
1    A           99      33       30
2    B           90      28       28
3    C           86      31       24
4    D           88      39       24
5    E           95      34       28

Notice that both methods produce the same result.

Example 2: Rename a Single Column Using dplyr

The following code shows how to rename the points column to total_points by name using the rename_at() function in :

library(dplyr)

#rename 'points' column to 'total_points' by name
df <- df %>% rename_at('points', ~'total_points')

#view updated data frame
df

  team total_points assists rebounds
1    A           99      33       30
2    B           90      28       28
3    C           86      31       24
4    D           88      39       24
5    E           95      34       28

The following code shows how to rename the points column to total_points by column position using the rename_at() function in :

library(dplyr)

#rename column in position 2 to 'total_points'
df <- df %>% rename_at(2, ~'total_points')

#view updated data frame
df

  team total_points assists rebounds
1    A           99      33       30
2    B           90      28       28
3    C           86      31       24
4    D           88      39       24
5    E           95      34       28

Notice that both methods produce the same result.

The following tutorials explain how to perform other common tasks in R:

x