如何使用R将国家/地区代码转换为数据框内的列中的国家/地区名称?

时间:2014-11-08 14:44:05

标签: r

 dd$country
  [1] US US US US GB US US HK US US US DE DE NL US US US US US CA CA FR FR DK CA GB AU AU IE LT PT AT US US US US US US US US US US US US US SG NL NL IT NL GB US US US NZ US GB GB US US US US ES IE ES
  [66] GB IE US US US US IE GB GB GB GB DE DE US FR AU IE US US US US GB GB GB GB GB GB US US IE GB GB GB GB HK US GB GB FR EU FR GB SE FI GB SE FI DK IT IE SE DK GB GB GB GB GB GB GB GB IE GB GB US US
  [131] US US US US CA GB GB NL IL US US US US US US US US US US US US US US US US US US US US US GB US US US US US US US US US US US US US US US US US US US US US US NL US US US US US US US US US US US
  [196] US US US US US ES US GB US US GB GB TR US US ES ES

  Levels: AT AU CA DE DK ES EU FI FR GB HK IE IL IT LT NL NZ PT SE SG TR US

3 个答案:

答案 0 :(得分:16)

您可以使用countrycode包。支持各种编码方案。看起来您的数据符合http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 countrycode表示为iso2c的数据。完整的国家/地区名称由country.name表示:

library(countrycode)
myCodes <- c("AT", "AU", "CA", "DE", "DK", "ES", "EU",
  "FI", "FR", "GB", "HK", "IE", "IL", "IT", "LT",
  "NL", "NZ", "PT", "SE", "SG", "TR", "US")
> countrycode(myCodes, "iso2c", "country.name")
[1] "Austria"        "Australia"      "Canada"         "Germany"       
[5] "Denmark"        "Spain"          NA               "Finland"       
[9] "France"         "United Kingdom" "Hong Kong"      "Ireland"       
[13] "Israel"         "Italy"          "Lithuania"      "Netherlands"   
[17] "New Zealand"    "Portugal"       "Sweden"         "Singapore"     
[21] "Turkey"         "United States" 

答案 1 :(得分:3)

jdharrison给出了一个很好的答案。

使用他的回答中的info / wiki页面,下面提供了一种匹配代码的替代方法 - 可能为替代方案添加一些值,其中代码表可在线获得,但没有匹配的包

使用包XML,您可以从Wikipedia网页中提取第3个表格,然后您可以将国家/地区代码与县名匹配。

library(XML)

wiki <- "http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2"

country <- readHTMLTable(wiki, header=TRUE, which=3, stringsAsFactors=FALSE)[1:2]

country$'Country name'[match(myCodes, country$Code)]
# [1] "Austria"        "Australia"      "Canada"         "Germany"       
# [5] "Denmark"        "Spain"          NA               "Finland"       
# [9] "France"         "United Kingdom" "Hong Kong"      "Ireland"       
# [13] "Israel"         "Italy"          "Lithuania"      "Netherlands"   
# [17] "New Zealand"    "Portugal"       "Sweden"         "Singapore"     
# [21] "Turkey"         "United States"

答案 2 :(得分:0)

对于希望对问题中的转换执行稍微不同的任何人(例如,我需要将国家/地区代码转换为货币代码),@jdharrison 非常出色解决方案将起作用,因为 countrycode 包有许多其他可以转换的变量,请查看所有 here

例如这里将国家代码转换为货币代码:

library(countrycode)
myCodes <- c("AT", "AU", "CA", "DE", "DK", "ES", "EU",
  "FI", "FR", "GB", "HK", "IE", "IL", "IT", "LT",
  "NL", "NZ", "PT", "SE", "SG", "TR", "US")

countrycode(myCodes, "iso2c", "iso4217c")