countrycode 确实内置了西班牙国家名称,但默认情况下它不能作为原始代码访问。您可以通过创建和使用自定义字典来解决此问题,如下例所示。缺点是它不会与正则表达式匹配,因此名称必须完全匹配(包括区分大小写)。如果您能够并且愿意为西班牙国家名称创建一组正则表达式,我们将非常高兴和感激将它们集成到 countrycode 作为默认的可访问源代码(可以在此处提交 https://github.com/vincentarelbundock/countrycode)。
library(countrycode)
custom_dict <- data.frame(spanish = countrycode::codelist$cldr.name.es,
english = countrycode::codelist$cldr.name.en,
stringsAsFactors = FALSE)
countries <- c("España", "Alemania")
countrycode(countries, "spanish", "english", custom_dict = custom_dict)
# [1] "Spain" "Germany"
它与您使用的数据中 92% 的国家/地区名称匹配,这至少是一个好的开始。您可以将不匹配的国家/地区名称的条目添加到自定义字典中以匹配所有这些条目。
library(countrycode)
url <- "https://catalogo.datos.gba.gob.ar/dataset/46b85203-17fe-42bd-b13f-1d3e150c06cd/resource/3eb20f55-7dc0-4671-a039-cf2e4b71c3db/download/expo_2016_2017.xlsx-expo.csv"
data <- read.csv(url, stringsAsFactors = FALSE)
custom_dict <- data.frame(spanish = countrycode::codelist$cldr.name.es,
english = countrycode::codelist$cldr.name.en,
stringsAsFactors = FALSE)
results <- countrycode(data$pais_descripcion, "spanish", "english", custom_dict = custom_dict)
sum(is.na(results))
# [1] 3902
sum(!is.na(results))
# [1] 45060