Skip to content

Instantly share code, notes, and snippets.

@davidcomfort
Created November 15, 2015 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidcomfort/7c6489b11cb8ff4c0cb9 to your computer and use it in GitHub Desktop.
Save davidcomfort/7c6489b11cb8ff4c0cb9 to your computer and use it in GitHub Desktop.
library(RCurl)
## Loading required package: bitops
countries <- getURL("https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.csv")
countries <- read.csv(text=countries, header = TRUE, stringsAsFactors=FALSE)
countries <- read.csv("data/countries.csv",
header = TRUE,
stringsAsFactors= FALSE, check.names=FALSE)
countries <- tbl_df(countries)
str(countries)[1:5]
## Classes 'tbl_df', 'tbl' and 'data.frame': 241 obs. of 9 variables:
## $ name : chr "Afghanistan" "Albania" "Algeria" "American Samoa" ...
## $ alpha-2 : chr "AF" "AL" "DZ" "AS" ...
## $ alpha-3 : chr "AFG" "ALB" "DZA" "ASM" ...
## $ country-code : int 4 8 12 16 20 24 660 28 32 51 ...
## $ iso_3166-2 : chr "ISO 3166-2:AF" "ISO 3166-2:AL" "ISO 3166-2:DZ" "ISO 3166-2:AS" ...
## $ region : chr "Asia" "Europe" "Africa" "Oceania" ...
## $ sub-region : chr "Southern Asia" "Southern Europe" "Northern Africa" "Polynesia" ...
## $ region-code : int 142 150 2 9 150 2 19 19 19 142 ...
## $ sub-region-code: int 34 39 15 61 39 17 29 29 5 145 ...
head(countries)[1:5]
## Source: local data frame [6 x 5]
##
## name alpha-2 alpha-3 country-code iso_3166-2
## (chr) (chr) (chr) (int) (chr)
## 1 Afghanistan AF AFG 4 ISO 3166-2:AF
## 2 Albania AL ALB 8 ISO 3166-2:AL
## 3 Algeria DZ DZA 12 ISO 3166-2:DZ
## 4 American Samoa AS ASM 16 ISO 3166-2:AS
## 5 Andorra AD AND 20 ISO 3166-2:AD
## 6 Angola AO AGO 24 ISO 3166-2:AO
colnames(countries)[1] <- c("Country")
colnames(countries)[4] <- c("Code")
colnames(countries)[6] <- c("Region")
colnames(countries)[7] <- c("Sub.Region")
countries <- transmute(countries, Country, Code, Region, Sub.Region)
head(countries)
## Source: local data frame [6 x 4]
##
## Country Code Region Sub.Region
## (chr) (int) (chr) (chr)
## 1 Afghanistan 4 Asia Southern Asia
## 2 Albania 8 Europe Southern Europe
## 3 Algeria 12 Africa Northern Africa
## 4 American Samoa 16 Oceania Polynesia
## 5 Andorra 20 Europe Southern Europe
## 6 Angola 24 Africa Middle Africa
str(countries)
## Classes 'tbl_df', 'tbl' and 'data.frame': 241 obs. of 4 variables:
## $ Country : chr "Afghanistan" "Albania" "Algeria" "American Samoa" ...
## $ Code : int 4 8 12 16 20 24 660 28 32 51 ...
## $ Region : chr "Asia" "Europe" "Africa" "Oceania" ...
## $ Sub.Region: chr "Southern Asia" "Southern Europe" "Northern Africa" "Polynesia" ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment