Table of Contents
The process of renaming multiple columns in a dataframe using dplyr in R involves using the “rename” function from the dplyr package. This function allows for the simultaneous renaming of multiple columns by providing a list of old and new column names as arguments. By utilizing this function, users can efficiently and easily rename columns in their dataframe without having to manually change each column name individually. This feature is particularly useful for manipulating and organizing large datasets in R.
Rename Multiple Columns Using dplyr
You can use the following functions from the package in R to rename multiple columns in a data frame:
Method 1: Use rename()
df %>% rename(new1 = old1, new2 = old2)
Method 2: Use rename_with()
new <- c('new1', 'new2')
old <- c('old1', 'old2')
df %>% rename_with(~ new, all_of(old))Both methods produce the same result.
The following examples show how to use each of these methods in practice with the following data frame in R:
#create data frame df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'), points=c(22, 34, 30, 12, 18), assists=c(7, 9, 9, 12, 14)) #view data frame df team points assists 1 A 22 7 2 B 34 9 3 C 30 9 4 D 12 12 5 E 18 14
Example 1: Rename Multiple Columns Using rename()
The following code shows how to use the rename() function to rename the team and points columns in the data frame:
library(dplyr)
#rename team and points columns
df2 <- df %>% rename(team_new = team, points_new = points)
#view updated data frame
df2
team_new points_new assists
1 A 22 7
2 B 34 9
3 C 30 9
4 D 12 12
5 E 18 14
The team and points columns have been renamed while the assists column has remained the same.
Example 2: Rename Multiple Columns Using rename_with()
The following code shows how to use the rename_with() function to rename the team and points columns in the data frame:
library(dplyr)
#define new names
new <- c('team_new', 'points_new')
#define old names to replace
old <- c('team', 'points')
#rename old names with new names
df2 <- df %>% rename_with(~ new, all_of(old))
#view updated data frame
df2
team_new points_new assists
1 A 22 7
2 B 34 9
3 C 30 9
4 D 12 12
5 E 18 14
The team and points columns have been renamed while the assists column has remained the same.
Note that this method may be easier to use when you have a long list of column names you’d like to replace.
The following tutorials explain how to perform other common tasks using dplyr:
Cite this article
stats writer (2024). How can I rename multiple columns in a dataframe using dplyr in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-rename-multiple-columns-in-a-dataframe-using-dplyr-in-r/
stats writer. "How can I rename multiple columns in a dataframe using dplyr in R?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-rename-multiple-columns-in-a-dataframe-using-dplyr-in-r/.
stats writer. "How can I rename multiple columns in a dataframe using dplyr in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-rename-multiple-columns-in-a-dataframe-using-dplyr-in-r/.
stats writer (2024) 'How can I rename multiple columns in a dataframe using dplyr in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-rename-multiple-columns-in-a-dataframe-using-dplyr-in-r/.
[1] stats writer, "How can I rename multiple columns in a dataframe using dplyr in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I rename multiple columns in a dataframe using dplyr in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
