Hi, thanks for trying to provide some properly formatted sample data but you have missed some details to make it actually usable by us, for future reference on how to provide a proper reproducible example, please read the guide on this link
About your problem at hand, you can use the case_when()
function from dplyr
, see this example (Please notice the way I'm sharing it with you, that would be a proper reprex).
library(dplyr)
# Sample data on a copy/paste friendly format
Data <- data.frame(
Variable = c("Germany", "Switzerland", "Bolivia", "Togo")
)
# Relevant code
Data %>%
mutate(
new_variable = case_when(
Variable == "Germany" ~ 1,
Variable == "Switzerland" ~ 2,
TRUE ~ 3
)
)
#> Variable new_variable
#> 1 Germany 1
#> 2 Switzerland 2
#> 3 Bolivia 3
#> 4 Togo 3
Created on 2022-04-30 by the reprex package (v2.0.1)