# Set libPaths.
.libPaths("/Users/kannishida/.exploratory/R/3.6")
# Load required packages.
library(MESS)
library(dslabs)
library(janitor)
library(lubridate)
library(hms)
library(tidyr)
library(stringr)
library(readr)
library(forcats)
library(RcppRoll)
library(dplyr)
library(tibble)
library(bit64)
library(exploratory)
# Steps to produce GDP
`GDP` <- exploratory::read_delim_file("/Users/kannishida/Downloads/API_NY.GDP.MKTP.CD_DS2_en_csv_v2_10134290/API_NY.GDP.MKTP.CD_DS2_en_csv_v2_10134290.csv" , ",", quote = "\"", skip = 4 , col_names = TRUE , na = c('','NA') , locale=readr::locale(encoding = "UTF-8", decimal_mark = "."), trim_ws = TRUE , progress = FALSE) %>%
readr::type_convert() %>%
exploratory::clean_data_frame() %>%
select(`Country Code`, `2017`) %>%
filter(!is.na(`2017`))
# Steps to produce GDP_tidy
`GDP_tidy` <- exploratory::read_delim_file("/Users/kannishida/Downloads/API_NY.GDP.MKTP.CD_DS2_en_csv_v2_10134290/API_NY.GDP.MKTP.CD_DS2_en_csv_v2_10134290.csv" , ",", quote = "\"", skip = 4 , col_names = TRUE , na = c('','NA') , locale=readr::locale(encoding = "UTF-8", decimal_mark = "."), trim_ws = TRUE , progress = FALSE) %>%
readr::type_convert() %>%
exploratory::clean_data_frame() %>%
select(-`Indicator Name`, -`Indicator Code`, -X63) %>%
gather(Year, GDP, `1960`:`2017`, na.rm = TRUE, convert = TRUE)
# Steps to produce the output
exploratory::read_excel_file( "/Users/kannishida/Dropbox/Data/country_population.xls", sheet = "Data", na = c('','NA'), skip=3, col_names=TRUE, trim_ws=TRUE, col_types="text") %>%
readr::type_convert() %>%
exploratory::clean_data_frame() %>%
select(-`Indicator Name`, -`Indicator Code`) %>%
gather(Year, Population, `1960`:`2018`, na.rm = TRUE, convert = TRUE) %>%
left_join(GDP_tidy, by = c("Country Code" = "Country Code", "Year" = "Year")) %>%
select(-`Country Name.y`) %>%
filter(!is.na(GDP)) %>%
mutate(Country_iso2c = countrycode(`Country Code`, origin="iso3c", destination="iso2c")) %>%
rename(Country_Name = `Country Name.x`, Country_iso3c = `Country Code`) %>%
reorder_cols(Country_Name, Country_iso3c, Country_iso2c)