How to Use rename_with() in R


Often you may want to rename several columns of a data frame in R by using a specific function that modifies the column names in a specific way.

One of the easiest ways to do this is by using the rename_with() function from the dplyr package in R, which is designed to perform this exact task.

The rename_with() function uses the following basic syntax:

rename_with(.data, .fn, .cols = everything(), …)

where:

  • .data: The name of the data frame
  • .fn: A function used to rename the selected columns
  • .cols: Columns to rename (default is all columns)

The following example shows how to use the rename_with() function from the dplyr package in practice.

Example: How to Use rename_with() in R

Suppose we create the following data frame that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 total_points=c(99, 68, 86, 88, 95, 74, 78, 93),
                 total_assists=c(22, 28, 45, 35, 34, 45, 28, 31),
                 total_rebounds=c(30, 28, 24, 24, 30, 36, 30, 29))

#view data frame
df

  team total_points total_assists total_rebounds
1    A           99            22             30
2    A           68            28             28
3    A           86            45             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

Suppose that we would like to convert all column names to uppercase.

We can use the toupper function with the rename_with() function to do so:

library(dplyr)

#rename all columns to uppercase
rename_with(df, toupper)

  TEAM TOTAL_POINTS TOTAL_ASSISTS TOTAL_REBOUNDS
1    A           99            22             30
2    A           68            28             28
3    A           86            45             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 each column name in the data frame is now shown in all uppercase letters.

We could also specify that we only want to convert specific columns to uppercase letters.

For example, we could use the following syntax to only rename columns that start with the string “total” to uppercase:

library(dplyr)

#rename specific columns to uppercase
rename_with(df, toupper, starts_with("total"))

  team TOTAL_POINTS TOTAL_ASSISTS TOTAL_REBOUNDS
1    A           99            22             30
2    A           68            28             28
3    A           86            45             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 only the columns that start with the string “total” have been renamed with all uppercase letters.

We can also perform more complex operations with the rename_with() function, such as replacing specific characters in each column name with a new character.

For example, we can use the following syntax with the gsub() function to replace each underscore in each column name with a dash instead:

library(dplyr)

#replace underscores with dashes in column names
rename_with(df, ~ gsub("_", "-", .x, fixed = TRUE))

  team total-points total-assists total-rebounds
1    A           99            22             30
2    A           68            28             28
3    A           86            45             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 each column that had an underscore now has a dash in the name instead.

Note that you can specify any pattern that you’d like to replace in the first argument of the gsub() function. You can then specify any pattern that you’d like to use as the replacement in the second argument of the gsub() function.

This is what makes the rename_with() function so powerful – you can use a wide variety of functions to rename several columns in a data frame at once.

Note: You can find the complete documentation for the rename_with() function from the dplyr package here.

Additional Resources

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

How to Use the pull() Function in dplyr
How to Use slice_max() in dplyr
How to Use top_n() in dplyr
How to Add Row to Data Frame Using dplyr

Featured Posts

Leave a Reply

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