How to Rename a Single Column in R (With Examples)


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 dplyr:

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 dplyr:

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.

Additional Resources

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

How to Select Specific Columns in R
How to Keep Certain Columns in R
How to Sort by Multiple Columns in R

Leave a Reply

Your email address will not be published.