R - 为什么我无法将国家/地区代码与自定义词典匹配?

问题描述 投票:0回答:1

我正在使用 gtap 数据,并希望将其与其他数据集结合起来。我正在尝试找到一种使用国家/地区 ID/代码的方法,似乎一个选择是使用 R 包

countrycodes
。但是,gtap 并不包含在软件包中支持的代码列表中。我试图创建一个自定义词典,但没有成功。

示例

gtap
数据:

gtap <- structure(list(COMM = c("coa", "coa", "coa", "coa", "coa", "coa"
), Source = c("afg", "afg", "afg", "afg", "afg", "afg"), Destination = c("afg", 
"alb", "are", "arg", "arm", "aus"), TotValue = c(9.99999997475243e-07, 
7.83022114774212e-05, 0.00216353917494416, 0.000611430441495031, 
2.76709855029367e-08, 2.72226079687243e-05)), row.names = c(NA, 
6L), class = "data.frame")

这是我尝试过的:

library(countrycode)
library(tidyverse)

get_dictionary()

cd <- get_dictionary("gtap10")

gtap_iso3c <- gtap %>% 
  mutate(countrycode(Source, "gtap.cha", "iso3c"))
Error in `mutate()`:
ℹ In argument: `countrycode(Source, "gtap.cha", "iso3c")`.
Caused by error in `countrycode()`:
! The `origin` argument must be a string of length 1 equal to one of these values: cctld, country.name, country.name.de, country.name.fr, country.name.it, cowc, cown, dhs, ecb, eurostat, fao, fips, gaul, genc2c, genc3c, genc3n, gwc, gwn, imf, ioc, iso2c, iso3c, iso3n, p5c, p5n, p4c, p4n, un, un_m49, unicode.symbol, unhcr, unpd, vdem, wb, wb_api2c, wb_api3c, wvs, country.name.en.regex, country.name.de.regex, country.name.fr.regex, country.name.it.regex.
Run `rlang::last_trace()` to see where the error occurred.
> 
r iso mutate country-codes
1个回答
0
投票

首先,为了使用带有

countrycode()
的自定义字典,必须使用参数
custom_dict = cd
,其中
cd
是包含匹配代码/名称的数据框。

但是,您使用的“gtap10”自定义词典不适合将“gtap.cha”与“iso3c”匹配... 1.因为它不包含iso3c代码,2.因为“gtap.cha”列包含大量重复值,因此不能用作“来源”,例如如果您从 gtap.cha -> Country.name 前往,“aus”将导致多个匹配项:澳大利亚、圣诞岛、科科斯(基林)群岛等。

dplyr::tibble(countrycode::get_dictionary("gtap10"))
#> # A tibble: 244 × 5
#>    country.name             country.name.en.regex    gtap.name gtap.num gtap.cha
#>    <chr>                    <chr>                    <chr>        <int> <chr>   
#>  1 Australia                "australia"              Australia        1 AUS     
#>  2 Christmas Island         "christmas"              Australia        1 AUS     
#>  3 Cocos (Keeling) Islands  "\\bcocos|keeling"       Australia        1 AUS     
#>  4 Heard & McDonald Islands "heard.*mcdonald"        Australia        1 AUS     
#>  5 Norfolk Island           "norfolk"                Australia        1 AUS     
#>  6 New Zealand              "new.?zealand"           New Zeal…        2 NZL     
#>  7 American Samoa           "^(?=.*americ).*samoa"   Rest of …        3 XOC     
#>  8 Cook Islands             "\\bcook"                Rest of …        3 XOC     
#>  9 Fiji                     "fiji"                   Rest of …        3 XOC     
#> 10 French Polynesia         "french.?polynesia|tahi… Rest of …        3 XOC     
#> # ℹ 234 more rows
© www.soinside.com 2019 - 2024. All rights reserved.