Get continent name from country name in R

前端 未结 3 809
猫巷女王i
猫巷女王i 2021-01-11 16:10

I have a data frame with one column representing country names. My goal is to add one more column which gives the continent information. Please check the following use case:

3条回答
  •  长情又很酷
    2021-01-11 16:38

    Expanding on Markus' answer, countrycode draws on codelists 'continent' declaration.

    ?codelist
    

    Definition of continent:

    continent: Continent as defined in the World Bank Development Indicators

    The question asked for continents but sometimes continents don't provide enough groups for you to delineate the data. For example, continents groups North and South America into Americas.

    What you might want is region:

    region: Regions as defined in the World Bank Development Indicators

    It is unclear how the World Bank groups regions but the below code shows how this destination is more granular.

    library(countrycode)
    
    egnations <- c("Afghanistan","Algeria","USA","France","New Zealand","Fantasyland")
    
    countrycode(sourcevar = egnations, origin = "country.name",destination = "region")
    

    Output:

    [1] "Southern Asia"            
    [2] "Northern Africa"          
    [3] "Northern America"         
    [4] "Western Europe"           
    [5] "Australia and New Zealand"
    [6] NA      
    

提交回复
热议问题