Often you may want to use the dplyr package in R to rename columns in a data frame.
Fortunately this is easy to do by using the rename() function, which is designed to perform this exact task.
You can use the following methods with the rename() function to rename columns in a data frame:
Method 1: Rename One Specific Column
library(dplyr)
#rename 'team' column to 'team_name'
df <- df %>% rename(team_name = team)
Method 2: Rename Multiple Specific Columns
library(dplyr)
#rename 'team' column to 'team_name' and 'points' to 'points_scored'
df <- df %>% rename(team_name = team,
points_scored = points))
Method 3: Rename All Columns
library(dplyr)
#rename all columns in data frame
df <- df %>% `colnames<-`(c("new1", "new2", "new3", "new4"))
The following examples show how to use the rename() function from the dplyr package to rename columns in a data frame, using the following data frame as an example:
#create data frame df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'), points=c(99, 68, 86, 88, 95, 74, 78, 93), assists=c(22, 28, 31, 35, 34, 45, 28, 31), rebounds=c(30, 28, 24, 24, 30, 36, 30, 29)) #view data frame df team points assists rebounds 1 A 99 22 30 2 A 68 28 28 3 A 86 31 24 4 A 88 35 24 5 B 95 34 30 6 B 74 45 36 7 B 78 28 30 8 B 93 31 29
Example 1: Rename One Specific Column Using dplyr
Suppose that we would like to rename the team column to team_name and leave all other column names unchanged.
We can use the following syntax to do so:
library(dplyr) #rename team column df <- df %>% rename(team_name = team) #view updated data frame df team_name points assists rebounds 1 A 99 22 30 2 A 68 28 28 3 A 86 31 24 4 A 88 35 24 5 B 95 34 30 6 B 74 45 36 7 B 78 28 30 8 B 93 31 29
Notice that the team column has been renamed while all other column names have been left the same.
Example 2: Rename Multiple Specific Column Using dplyr
Suppose that we would like to rename the team column to team_name and rename the points column to points_scored and leave all other column names unchanged.
We can use the following syntax to do so:
library(dplyr) #rename team column df <- df %>% rename(team_name = team, points_scored = points) #view updated data frame df team_name points_scored assists rebounds 1 A 99 22 30 2 A 68 28 28 3 A 86 31 24 4 A 88 35 24 5 B 95 34 30 6 B 74 45 36 7 B 78 28 30 8 B 93 31 29
Notice that the team and points columns have been renamed while all other column names have been left the same.
Example 3: Rename All Columns Using dplyr
Suppose that we would like to rename every column in the data frame.
We can use the following syntax to do so:
library(dplyr) #rename all columns in data frame df <- df %>% `colnames<-`(c("new1", "new2", "new3", "new4")) #view updated data frame df new1 new2 new3 new4 1 A 99 22 30 2 A 68 28 28 3 A 86 31 24 4 A 88 35 24 5 B 95 34 30 6 B 74 45 36 7 B 78 28 30 8 B 93 31 29
Notice that all column names have been renamed based on the names that we specified.
Note #1: The number of column names specified using the colnames function must contain the same number of names as the number of existing columns in the data frame.
Note #2: You can find the complete documentation for the rename() function from the dplyr package here.
Additional Resources
The following tutorials explain how to perform other common tasks in R:
How to Add Row to Data Frame Using dplyr
How to Arrange Rows in Custom Order Using dplyr
How to Filter Based on Factor in dplyr
How to Use top_n() in dplyr