I'm redoing some old university essays to convert it from Stata to R to learn R, and am trying to find a way to take a variable of countries, and create a new variable with a factor of being an OECD member or Non-member.
I have tried to mutate from dplyr with an if-else statement, but it does not seem to work, and all the googling I have tried seem to just show releveling existing factors and such.
As an example (country_name contains names of all the countries in the world, shortended for ease of readability):
df <- data.frame(country_name = c("Australia", "Austria", "Belgium", "Algeria", "Bahrain", "Comoros"))
df <- df %>% mutate(OECD = ifelse(country_name = c("Australia", "Austria", "Belgium"), "OECD", "Non-OECD")
I would have expected that the row containing "Australia", "Austria", "Belgium" would have gotten the factor level of OECD, while "Algeria", "Bahrain", "Comoros" would have gotten Non-OECD.
Ps, as this is my second question ever, any tips on how to improve my question making are much appreciated!
SOLVED: As pointed out in the accepted answer below, I should have used the %in% operator. Many thanks!