从R中的国家名称获取世界区域名称
在我的数据中,有一列是国家名称。我想创建一个新变量,根据excel表格列出每个国家所在的地区,我在表格中按地区标记了每个国家从R中的国家名称获取世界区域名称,r,region,country,R,Region,Country,在我的数据中,有一列是国家名称。我想创建一个新变量,根据excel表格列出每个国家所在的地区,我在表格中按地区标记了每个国家 我不想使用countrycode包,因为它没有足够的特定区域(即,它将荷兰标记为欧洲,而不是北欧)。有没有办法让R检查一个单元格并将该单元格的内容与另一个数据集相匹配 将电子表格导入R(使用RExcel,或导出为CSV,并使用基本函数导入)。假设电子表格有两列,分别命名为国家和地区,如下所示: regions <- data.frame(Country = c("G
我不想使用countrycode包,因为它没有足够的特定区域(即,它将荷兰标记为欧洲,而不是北欧)。有没有办法让R检查一个单元格并将该单元格的内容与另一个数据集相匹配 将电子表格导入R(使用RExcel,或导出为CSV,并使用基本函数导入)。假设电子表格有两列,分别命名为
国家
和地区
,如下所示:
regions <- data.frame(Country = c("Greece", "Netherlands"),
Region = c("Southern Europe", "Northern Europe"),
stringsAsFactors = FALSE)
regions
#> Country Region
#> 1 Greece Southern Europe
#> 2 Netherlands Northern Europe
named <- regions$Region
names(named) <- regions$Country
named
#> Greece Netherlands
#> "Southern Europe" "Northern Europe"
other <- c("Netherlands", "Greece", "Greece")
named[other]
#> Netherlands Greece Greece
#> "Northern Europe" "Southern Europe" "Southern Europe"
other2 <- c("Greece", "France")
named[other2]
#> Greece <NA>
#> "Southern Europe" NA