I have a factor variable with countries. I have to use ! and %in% operators so that I can keep the "United States", "Switzerland", "United Kingdom" and transform the rest to "Others". But the code I am using is not working
country <- c(rep(x = "United States", 466), rep(x = "United Kingdom", 250), rep(x = "Switzerland", 520),
rep(x = "France", 97), rep(x = "Italy", 85), rep(x = "Germant", 39), rep(x = "Canada", 25),
rep(x = "Singapore", 2), rep(x = "South Africa", 9))
country
bulk <- c("United States", "Switzerland", "United Kingdom")
if(! bulk %in% country) country <- "Others"
I am expecting it to make four categories. United States, Switzerland, United Kingdom, Others. But I don't want the solution out of context of "!" and "%in%" operators.
!and/or%in%?country[country %in% bulk] <- "Others"?