You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言问题:无法将GTAP国家代码匹配到自定义字典?

GTAP编码转ISO3C:解决countrycode不支持的问题

问题说明

处理GTAP贸易数据时,需要将GTAP国家编码转换为ISO3C编码以合并其他数据集,但countrycode包原生不支持GTAP编码体系,调用get_dictionary("gtap10")无效,直接指定origin = "gtap.cha"会报错。

示例数据

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.

解决方案

因为countrycode没有内置GTAP编码字典,需要手动构建映射表,再通过两种方式完成转换:

1. 自定义字典配合countrycode函数

countrycode支持通过custom_dict参数传入自定义编码映射,步骤如下:

  • 构建映射表:从GTAP官方文档获取完整编码,以下是示例:
gtap_to_iso3c <- tibble(
  gtap_code = c("afg", "alb", "are", "arg", "arm", "aus"),
  iso3c = c("AFG", "ALB", "ARE", "ARG", "ARM", "AUS")
)
  • 转换编码:
gtap_iso3c <- gtap %>%
  mutate(
    Source_iso3c = countrycode(Source, 
                              origin = "gtap_code", 
                              destination = "iso3c",
                              custom_dict = gtap_to_iso3c),
    Destination_iso3c = countrycode(Destination, 
                                    origin = "gtap_code", 
                                    destination = "iso3c",
                                    custom_dict = gtap_to_iso3c)
  )

2. 直接用left_join匹配

如果不需要countrycode的额外功能,用dplyr的left_join更直观:

gtap_iso3c <- gtap %>%
  left_join(gtap_to_iso3c, by = c("Source" = "gtap_code")) %>%
  rename(Source_iso3c = iso3c) %>%
  left_join(gtap_to_iso3c, by = c("Destination" = "gtap_code")) %>%
  rename(Destination_iso3c = iso3c)

完整可运行代码

library(tidyverse)
library(countrycode)

# 示例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")

# 自定义GTAP到ISO3C映射(需补充完整编码)
gtap_to_iso3c <- tibble(
  gtap_code = c("afg", "alb", "are", "arg", "arm", "aus"),
  iso3c = c("AFG", "ALB", "ARE", "ARG", "ARM", "AUS")
)

# 方法1:使用countrycode自定义字典
gtap_result1 <- gtap %>%
  mutate(
    Source_iso3c = countrycode(Source, "gtap_code", "iso3c", custom_dict = gtap_to_iso3c),
    Destination_iso3c = countrycode(Destination, "gtap_code", "iso3c", custom_dict = gtap_to_iso3c)
  )

# 方法2:使用left_join
gtap_result2 <- gtap %>%
  left_join(gtap_to_iso3c, by = c("Source" = "gtap_code")) %>%
  rename(Source_iso3c = iso3c) %>%
  left_join(gtap_to_iso3c, by = c("Destination" = "gtap_code")) %>%
  rename(Destination_iso3c = iso3c)

# 输出结果
print(gtap_result1)

内容的提问来源于stack exchange,提问作者MoonS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 09:16:01