Skip to content

Commit

Permalink
Adding missing country ISO code (closes #213, #243).
Browse files Browse the repository at this point in the history
  • Loading branch information
PMassicotte committed Dec 18, 2017
1 parent d8f1cd0 commit aab5930
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 15 deletions.
Binary file modified R/sysdata.rda
Binary file not shown.
23 changes: 8 additions & 15 deletions data-raw/process_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ get_countries <- function() {
destfile <- tempfile(fileext = ".csv")

ret <- download.file(
"https://raw.githubusercontent.com/olahol/iso-3166-2.json/master/data/eQuest.csv",
"https://raw.githubusercontent.com/CharlotteWoolley/Comprehensive_ISO_Location_Codes/master/ISO_codes.csv",
destfile
)

# Was the file found?
stopifnot(ret == 0)

# , col.names = c("country_code", "sub_code", "country")
df <- read.csv(destfile, sep = ",", header = FALSE, fileEncoding = "utf8", stringsAsFactors = FALSE)
df <- tidyr::unite(df, "sub_code", V2, V3, sep = "")
names(df) <- c("country_code", "sub_code", "country")
countries <- read.csv(destfile)

# Fix the encoding
countries <- data.frame(sapply(countries, iconv, to = "ASCII//TRANSLIT"))

# *************************************************************************
# USA metro codes
Expand All @@ -60,24 +60,17 @@ get_countries <- function() {
lapply(regmatches(usa$Metro, regexec(", (\\S{2})", usa$Metro)), "[", 2),
usa$`Metro code`,
sep = "-"
), stringsAsFactors = FALSE
)
)

usa <- na.omit(usa)
usa <- usa[, c(1, 3, 2)]
names(usa) <- names(countries)

# *************************************************************************
# Merge together
# *************************************************************************
countries <- dplyr::bind_rows(df, usa)

# *************************************************************************
# Final format
# *************************************************************************
countries$country_code <- ifelse(countries$country_code == "", NA, countries$country_code)
countries <- tidyr::fill(countries, country_code)

# Fix the encoding
countries <- data.frame(sapply(countries, iconv, to = "ASCII//TRANSLIT"))
countries <- rbind(countries, usa)

return(countries)

Expand Down
Binary file modified data/categories.rda
Binary file not shown.
Binary file modified data/countries.rda
Binary file not shown.

0 comments on commit aab5930

Please sign in to comment.