• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / R / How to Replace Multiple Column Names of a Dataframe with tidyverse

How to Replace Multiple Column Names of a Dataframe with tidyverse

March 1, 2022 by cmdlinetips

Of late, I am renaming column names of a dataframe a lot, in different flavors, in R using tidyverse. And every time I have to google it up :). Just came across, a really neat trick from Shannon Pileggi on twitter to replace multiple column names using deframe() function and !!! splice operator. Here is a quick post for this more general version of renaming column names for future self.

Often the base R way of using colnames() to change the names might work if you the column names are in the same order as the new name vector.

colnames(df) <- new_name_vector

If it does, well and good. Sometimes I need a solution in tidyverse way. One of the ways I have been renaming the column names using rename_with() function in tidyverse. This approach is bit limited as it is useful mainly for some kind of string match and replace.

First, let us create a toy data frame with column names that we would like to change to some new names.

library(tidyverse)
df <- tibble(var_1 = letters[1:5],
             var_2 = sample(5),
             var_3 = sample(5),
             var_4 = sample(5))

The tibble we created has four columns and for the sake of ease, all the names have some common string.

df

## # A tibble: 5 × 4
##   var_1 var_2 var_3 var_4
##   <chr> <int> <int> <int>
## 1 a         4     3     2
## 2 b         1     4     4
## 3 c         5     2     3
## 4 d         2     5     5
## 5 e         3     1     1

Renaming multiple column names using rename_with() function

If we wanted to replace the common string in the names to something else, we can use rename_with() function in combination with substitute function like gsub to replace a pattern.

df %>%
  rename_with(function(x){gsub("var","variable",x)})

In this example, we have changed “var” in column names to “variable”. In the above solution rename columns, we were replacing any occurrence of “var” with “variable”.

## # A tibble: 5 × 4
##   variable_1 variable_2 variable_3 variable_4
##   <chr>           <int>      <int>      <int>
## 1 a                   4          3          2
## 2 b                   1          4          4
## 3 c                   5          2          3
## 4 d                   2          5          5
## 5 e                   3          1          1

However, what if we don’t want to simply replace a part of a string in new column names. The above approach is not useful as of now. Ideally we should be able to provide a character/string vector of interest as new column names.

Renaming multiple column names using named character vector and !!! splice operator

A better solution would be to have a dictionary (like Pandas rename() function in Python) or a lookup table containing current column name and the new column names. The neat tip shared by Shannon Pileggi on twitter does exactly that. One of the biggest advantage with this approach is that we can change the column names to anything we like. For example, if we want to rename the first column name to “ID” and the rest to “variable_*”, here is how to do it.

We need to create a dataframe with two columns, one containing new column names and the second containig old column names and

df_colnames <- tibble(
  "new_name" = c("ID",paste0("variable_",2:4)),
  "old_name" = colnames(df)
)
df_colnames

## # A tibble: 4 × 2
##   new_name   old_name
##   <chr>      <chr>   
## 1 ID         var_1   
## 2 variable_2 var_2   
## 3 variable_3 var_3   
## 4 variable_4 var_4

Then we will use the deframe() function in tibble to convert the two-column data frames to a named vector.

var_names <- deframe(df_colnames)

deframe() function uses first column as name and the second column as value.

var_names

##         ID variable_2 variable_3 variable_4 
##    "var_1"    "var_2"    "var_3"    "var_4"

Then we can use rename() function using. the magic “!!!” slice operator on the named vector that we just created to replace the old column names to the new names.

df %>% 
   rename(!!!var_names)

## # A tibble: 5 × 4
##   ID    variable_2 variable_3 variable_4
##   <chr>      <int>      <int>      <int>
## 1 a              1          2          5
## 2 b              3          4          1
## 3 c              5          1          3
## 4 d              4          3          4
## 5 e              2          5          2

Isn’t this nice?

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Replace NAs with Column/Row MeanHow to Replace NAs with column mean or row means with tidyverse Default Thumbnail7 Tips to Add Columns to a DataFrame with add_column() in tidyverse Pandas Replace Multiple Column Values with DictionaryHow to Replace Multiple Column Values with Dictionary in Python How to Compute Column Means in R?How To Compute Column Means in R with tidyverse

Filed Under: R, R Tips, tidyverse 101 Tagged With: rename column names rename_with(), rename column names using deframe and !!!, rename multiple column names tidyverse

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version