r - 是否可以在国家代码包中区分美洲?
问题描述
我有一个大型数据框,在 countrycode 包的帮助下,我将各大洲分配给了可用的 ISO 字符。现在我想区分北美和南美,因为它们是两个不同的大陆,而且似乎套餐中没有这样做的选项。有没有人知道如何解决这个问题?
解决方案
我刚刚发现还有一个叫做“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=)
推荐阅读
- java - bean 类 [java.math.BigInteger] 的无效属性“计数”:在回退访问期间找不到属性字段
- javascript - Filepond 在一个请求中处理所有文件
- amazon-cloudfront - 无法将 https://*.cloudfront.net 添加到 IE“受信任的站点”
- python - 使用 keras 进行单元测试
- python-3.x - 如何使用 Opencv 在 Pyqt 中显示图像
- java - 可以将 PrintWriters 放在 java List 或 Collection 中吗?
- function - 用于时间序列分析的缩放/参数加权函数
- javascript - 如何在一个元素中创建具有多个值的可点击列表?
- android - 如何使用 Arduino NFC 从 Android 接收密钥并在有效时做出响应?
- sql-server - 从 cmd 行构建 SSIS 2014 项目部署模型