Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert country codes into country names in a column within a data frame using R?

Tags:

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
like image 640
user3563667 Avatar asked Nov 08 '14 14:11

user3563667


3 Answers

You can use the countrycode package. Various coding schemes are supported. It looks like you have data conforming to http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 which countrycode denotes as iso2c. Full country names are denoted by 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" 
like image 65
jdharrison Avatar answered Nov 17 '22 01:11

jdharrison


jdharrison gives a great answer.

Using the info / wiki page from his answer, below gives an alternative way to match the codes - perhaps adds a little value for alternative scenarios where a table of codes is available online but no r package to match

Using package XML you can extract the 3rd table fro the Wikipedia webpage - you can then match country codes with county names.

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"
like image 21
user20650 Avatar answered Nov 17 '22 00:11

user20650


For anyone wishing to perform a slightly different conversion to the one in the question (for example, I needed to convert country codes to currency codes), @jdharrison's excellent solution will work since the countrycode package has many other variables that can be converted to, see them all here

For example here's converting country code to currency code:

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")
like image 1
stevec Avatar answered Nov 16 '22 23:11

stevec