I am trying to rename some countries in my data but I seem to be having an issue with one. I wish to rename these following countries but I get an error when trying to rename 'Egypt, Arab Rep.' to `Egypt'
data
Country_Name value
Egypt, Arab Rep. 2192
Syrian Arab Republic 4998
Turkiye 8230
code used to rename
data = data %>% rename(Egypt = "Egypt, Arab Rep.") %>% rename(Syria = "Syrian Arab Republic") %>% rename(Turkey = "Turkiye")
error message received
`Error in `stop_subscript()`:
! Can't rename columns that don't exist.
✖ Column_Name `Egypt, Arab Rep.` doesn't exist.`
CodePudding user response:
A dplyr
way of doing this would be using the case_when
function
library(dplyr)
df <- data.frame(Country = c("Egypt, Arab Rep.", "Syrian Arab Republic", "Turkiye"),
value = c(2192, 4998, 8230))
df <- df %>%
dplyr::mutate(Country = dplyr::case_when(
Country == "Egypt, Arab Rep." ~ "Egypt",
Country == "Syrian Arab Republic" ~ "Syria",
TRUE ~ Country
))
RESULT:
Country value
1 Egypt 2192
2 Syria 4998
3 Turkiye 8230