Reputation: 93
I have a data frame in R containing a column "CountryCode".
I would like to select valid Countries and replace all other codes with "OtherCountry". So I wrote:
Valid_Countries <- c("US", "CA", "JP", "AU", "DE", "IT", "ES", "FR", "UK", "FI", "SE", "NO")
levels(Orders2$CountryCode) <- gsub(paste0("[^", paste(Valid_Countries, collapse=""), "]+"), "OtherCountry", levels(Orders2$CountryCode))
which almost works. My problem is that Country codes like "BE" is replaced with "OtherCountryE" (I guess it is because "E" is included in the Valid_Countries).
How can I say "just consider the entire code"?
Upvotes: 0
Views: 92
Reputation: 3239
Does this work:
levels(Orders2$CountryCode)[
!(levels(Orders2$CountryCode) %in% Valid_Countries)
] <- "OtherCountry
Upvotes: 1