【问题标题】:Is there an option to differentiate the americas in the countrycode package?是否可以选择在 countrycode 包中区分美洲?
【发布时间】:2019-10-22 23:17:27
【问题描述】:

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

【问题讨论】:

  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。

标签: r country-codes


【解决方案1】:

我刚刚发现还有一个名为“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=)

【讨论】:

    猜你喜欢
    • 2011-11-26
    • 1970-01-01
    • 2022-07-26
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    相关资源
    最近更新 更多