str_replace_all by grouping

42 Views Asked by At

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

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

2

There are 2 best solutions below

0
LMc On BEST ANSWER

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))
0
jpsmith On

Although you tagged tidyverse, for others a base R approach with match:

ix <- match(df$team_name, names(teams_names_replace))

df$team_name_long <- ifelse(!is.na(ix), teams_names_replace[ix], df$team_name)

#   country team_name     team_name_long     
#   <chr>   <chr>         <chr>              
# 1 ENG     Huddersfield  Huddersfield Town  
# 2 ESP     Betis         Real Betis         
# 3 ITA     Inter         Internazionale     
# 4 GER     Leverkusen    Bayer Leverkusen   
# 5 FRA     Paris S-G     Paris Saint-Germain
# 6 BRA     Internacional Internacional