首页 > 解决方案 > 是否可以在国家代码包中区分美洲?

问题描述

我有一个大型数据框,在 countrycode 包的帮助下,我将各大洲分配给了可用的 ISO 字符。现在我想区分北美和南美,因为它们是两个不同的大陆,而且似乎套餐中没有这样做的选项。有没有人知道如何解决这个问题?

标签: rcountry-codes

解决方案


我刚刚发现还有一个叫做“raster”的包,它有更详细的大陆信息,可以用来代替“countrycode”。使用 (countrycode),也可以单独指定某些国家/地区。

它甚至允许在更详细的区域之后进行分组。

两个包的示例:

      # 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=)

推荐阅读