Search code examples
rif-statementindexingdplyrrecode

A better way to do over 150 country recodes?


I'm trying to recode a variable consisting of the recognised countries into regions that I am specifying. I've tried to do multiple if_else statements using dplyr to recode country variable into regions, but it is getting ridiculously long.

I would like to use for loop to loop through several countries in a vector and change the value to something new, in the example below I'd like to change the values in df$country that match i and change it to "Europe". This is the code I've developed, which doesn't seem to want to work. Is there a better way to do this?

df <- data.frame(country =c("Netherlands", "US", "Canada", "Frace", "Italy"),
       ID=1:100)
i <- c("Netherlands", "France", "Italy")
n <- length(i)

for (i in n){
df$country[(df$country == i)] <- "Europe"
}

I'm getting several different errors depending on how I change the formatting. It seems that nothing will work.


Solution

  • As the 'country' is factor, we can assign the levels of the 'country' that are %in% 'i' to 'Europe' instead of using a for loop

    levels(df$country)[levels(df$country) %in% i] <- "Europe"