Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Dplyr: Rename multiple variables with regex by name

I need to rename multiple variables using a replacement dataframe. This replacement dataframe also includes regex. I would like to use a similar solution proposed here, .e.g

df %>% rename_with(~ newnames, all_of(oldnames))

MWE:

df <- mtcars[, 1:5]

# works without regex 
replace_df_1 <- tibble::tibble(
  old = df %>% colnames(),
  new = df %>% colnames() %>% toupper()
) 

df %>% rename_with(~ replace_df_1$new, all_of(replace_df_1$old))


# with regex

replace_df_2 <- tibble::tibble(
  old = c("^m", "cyl101|cyl", "disp", "hp", "drat"),
  new = df %>% colnames() %>% toupper()
)

  old        new  
  <chr>      <chr>
1 ^m         MPG  
2 cyl101|cyl CYL  
3 disp       DISP 
4 hp         HP   
5 drat       DRAT 

# does not work
df %>% rename_with(~ replace_df_2$new, all_of(replace_df_2$old))
df %>% rename_with(~ matches(replace_df_2$new), all_of(replace_df_2$old))

Thank you for any hints!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

matches should be on the regex-y column:

df %>% 
  rename_with(~ replace_df_2$new, matches(replace_df_2$old))
                     MPG CYL  DISP  HP DRAT
Mazda RX4           21.0   6 160.0 110 3.90
Mazda RX4 Wag       21.0   6 160.0 110 3.90
Datsun 710          22.8   4 108.0  93 3.85
Hornet 4 Drive      21.4   6 258.0 110 3.08
Hornet Sportabout   18.7   8 360.0 175 3.15
Valiant             18.1   6 225.0 105 2.76
#...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading