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

str_replace_all by grouping

I need to replace abbreviated names with long ones.

However, I’m getting the wrong behavior in a case that shouldn’t be replaced. This replacement should respect the country condition.

enter image description here

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

I’m using:

library(tidyverse)

df <- structure(list(country = c("ENG", "ESP", "ITA", "GER", "FRA", 
"BRA"), team_name = c("Huddersfield", "Betis", "Inter", "Leverkusen", 
"Paris S-G", "Internacional")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L))

teams_names_replace <-
  c(
    "Huddersfield" = "Huddersfield Town",
    "Inter" = "Internazionale",
    "Paris S-G" = "Paris Saint-Germain",
    "Betis" = "Real Betis",
    "Leverkusen" = "Bayer Leverkusen"
  )

df %>%
  mutate(team_name_long = str_replace_all(
    team_name,
    c(teams_names_replace)))

*group_by(country) does not work

>Solution :

Any reason you don’t just use teams_names_replace as a look up table:

library(dplyr)

df |> 
  mutate(team_name_long = teams_names_replace[team_name])

And if you want to keep the team name if no match is found you can do the following instead:

df |> 
  mutate(team_name_long = coalesce(teams_names_replace[team_name], team_name))
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