【问题标题】:Translating Country Names from Spanish to English with translateR将国家名称从西班牙语翻译成英语并进行翻译
【发布时间】:2019-01-17 18:00:53
【问题描述】:

我有一个包含国家名称和出口的数据集,但国家名称是西班牙语。我想使用countrycode 来获取正确的代码来映​​射它,但是countrycode 只能从英语/德语转换为其他语言(而不是相反)。我尝试使用translateR 将西班牙语名称更改为英语,但新列输出的内容与原始列完全相同。

我认为这不是我的 API,因为我最初遇到了一个不同的错误,但后来我重新启动了 R,它就消失了。是代码里的东西吗?

#read in file
data <- read.csv("...", header = TRUE)
data$char <- as.character(data$pais_descripcion)

#translate
library(translateR)

google.dataset.out <- translate(dataset = data,
content.field = 'char',
google.api.key = 'key',
source.lang = 'es',
target.lang = 'en')

Dataset with countries(pais_descripcion)

【问题讨论】:

    标签: r country-codes


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      相关资源
      最近更新 更多