2

I have a large dataframe where I assigned the continents to the available ISO characters with help of the countrycode package. Now I would like to differentiate between north and south america as they are two different continents and it seems the package includes no option to do so. Has anyone got an idea on how to solve this problem?

laesas
  • 71
  • 7
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 22 '19 at 14:56

1 Answers1

2

I just found that there is another package called "raster" which has more detailed continent information and can be used instead of "countrycode". With (countrycode), one can assign certain countries individually as well.

It even allows grouping after more detailed regions.

Examples for both packages:

      # Assigning continents to country codes 

library(countrycode)

iso2c_to_continents <- c(CA = "North America" , US = "North America")
c <- countrycode(sourcevar = yourdf[["ISO Code"]], origin = "iso2c", destination = "continent",custom_match = iso2c_to_continents)

yourdf <- cbind(yourdf, c)

    # if the destination should be a more detailed region, little different:
c.2 <- countrycode(sourcevar = yourdf[["ISO Code"]], origin = "iso2c", destination = "region")
yourdf <- cbind(yourdf, c.2)

    # Another package could be used as well

library(raster)

yourdf <- merge(yourdf,ccodes()[,c("ISO2","continent")],by.x=,by.y=)

# or

yourdf <- merge(yourdf,ccodes()[,c("ISO2","UNREGION1")],by.x=,by.y=)
laesas
  • 71
  • 7