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.
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
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))