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

Use of across with str_ within a mutate

I’d like to apply a character replacement to all columns of a tibble. I can do it for one column:

starwars %>% mutate(name = str_replace_all(name, "-", ""))

But I do not understand how to generalise to with across():

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

starwars %>% mutate(
  across(everything(),
    str_replace_all("-", "")
    )
  )

Is it necessary to use the purr package here?

>Solution :

You almost had it. Use the dot notation to dynamically pass all columns specified in the everything() command to your string function.

library(tidyverse)
starwars %>%
  mutate(across(everything(),
         ~str_replace_all(., "-", "")))
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