-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tidyverse tools, style guide, countrycode package
* use `tidyverse` tools, esp. `read_csv()`, `write_csv()` * use tidyverse style guide, esp. " ' " --> ' " ' * use `countrycode` package, instead of `country.csv`
- Loading branch information
Showing
14 changed files
with
92 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
library('dplyr') | ||
library(tidyverse) | ||
library(countrycode) | ||
|
||
ees <- read.csv('parties-ees-ches-ess.csv', fileEncoding = 'utf-8', as.is=TRUE) | ||
ees_raw <- read_csv("parties-ees-ches-ess.csv") | ||
|
||
country <- read.csv('../country.csv', fileEncoding = 'utf-8', as.is=TRUE) | ||
country <- country %>% select(iso2, country_iso3 = iso3) | ||
ees <- ees_raw %>% | ||
mutate(country_iso3 = countrycode(country, "iso2c", "iso3c", | ||
custom_match = c(UK="GBR"))) | ||
|
||
ees[ees$country == 'UK', 'country'] <- 'GB' | ||
ees <- ees %>% left_join(country, by = c('country' = 'iso2')) | ||
|
||
write.csv(ees, 'ees14.csv', na='', fileEncoding = 'utf-8', row.names = FALSE) | ||
write_csv(ees, "ees14.csv", na = "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
library("tidyverse") | ||
library("readxl") | ||
library("countrycode") | ||
library(tidyverse) | ||
library(readxl) | ||
library(countrycode) | ||
|
||
epac_raw <- read_excel("epac-parties-2016.xlsx") | ||
write_csv(epac_raw, "epac-parties-2016.csv", na = "") | ||
|
||
# add Party Facts country codes | ||
epac <- epac_raw %>% | ||
mutate(country = countrycode(country_name, 'country.name', 'iso3c', | ||
custom_match = c(Kosovo='XKX')), | ||
mutate(country = countrycode(country_name, "country.name", "iso3c", | ||
custom_match = c(Kosovo="XKX")), | ||
seat = round(seat, 1)) | ||
if(any(is.na(epac$country))) { | ||
warning("Country name clean-up needed") | ||
} | ||
|
||
epac_2014_raw <- read_csv("import-2014/epac.csv") | ||
|
||
epac_2014_add <- epac_2014_raw %>% | ||
rename(country = country_name_short, country_name = country, party_id = id, | ||
party_accr = accronym, party_name_en = party_name_english) %>% | ||
mutate(round = 2011, pec = NA, elecyear = NA) %>% | ||
epac_2014_add <- epac_2014_raw %>% | ||
rename(country = country_name_short, country_name = country, party_id = id, | ||
party_accr = accronym, party_name_en = party_name_english) %>% | ||
mutate(round = 2011, pec = NA, elecyear = NA) %>% | ||
filter( ! party_id %in% epac$party_id) | ||
|
||
epac <- epac %>% bind_rows(epac_2014_add) %>% arrange(party_id) | ||
|
||
write.csv(epac, "epac.csv", na='', fileEncoding = "utf-8", row.names = FALSE) | ||
write.csv(epac, "epac.csv", na = "", fileEncoding = "utf-8", row.names = FALSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,11 @@ | ||
library('dplyr') | ||
library(tidyverse) | ||
library(countrycode) | ||
|
||
# reading huber data and renaming 'id' to 'party_id' | ||
party_raw <- read.csv('huber_inglehart_1995.csv', as.is=TRUE) | ||
party <- party_raw %>% rename(party_id=id) | ||
huber_raw <- read_csv("huber_inglehart_1995.csv") | ||
|
||
# reading contry data and convert 'country_name' to upper-case characters | ||
country_raw <- read.csv('../country.csv', fileEncoding = 'utf-8', as.is=TRUE) | ||
country <- country_raw %>% | ||
rename(country_name_short = name_short) %>% | ||
mutate(country = toupper(name)) | ||
huber <- huber_raw %>% | ||
rename(huber_id=id) %>% | ||
mutate(country_name_short = countrycode(country, "country.name", "iso3c", | ||
custom_match = c(`NORTHERN IRELAND`="NIR"))) | ||
|
||
# merging country and huber data to get 'country_name_short' | ||
party <- party %>% left_join(country %>% select(country_name_short, country), by='country') | ||
|
||
# adding missing country abbreviations | ||
country_update <- list('BRITAIN'='GBR', 'SOUTH KOREA'='KOR', 'USA'='USA') | ||
for (to_update in names(country_update)) { | ||
party[party$country == to_update, 'country_name_short'] <- country_update[[to_update]] | ||
} | ||
if(any(is.na(party$country_name_short))) warning("Not all observations have country keys") | ||
|
||
# creating the csv file | ||
write.csv(party, 'huber.csv', na='', fileEncoding='utf-8', row.names = FALSE) | ||
write_csv(huber, "huber.csv", na = "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
library("dplyr") | ||
library(tidyverse) | ||
|
||
janda <- read.csv('janda-parties.csv', fileEncoding='utf-8', as.is=TRUE) | ||
country <- read.csv('janda-country.csv', fileEncoding='utf-8', as.is=TRUE) | ||
janda <- read_csv("janda-parties.csv") | ||
country <- read_csv("janda-country.csv") | ||
|
||
# Extract country id from party id | ||
janda <- janda %>% | ||
mutate(country_id = substr(janda_id, 1, nchar(janda_id) - 1) %>% as.integer, | ||
country_id = ifelse(janda_id >= 10, country_id, 0)) # add US country id '0' | ||
country_id = ifelse(janda_id >= 10, country_id, 0)) # add US country id "0" | ||
|
||
# Merge parties and country list | ||
janda <- janda %>% | ||
janda <- janda %>% | ||
left_join(country, by = c("country_id" = "id")) %>% | ||
select(-country_id, country_short = short) %>% | ||
filter(country_short != '') | ||
filter(country_short != "") | ||
|
||
write.csv(janda, "janda.csv", na='', fileEncoding = "utf-8", row.names = FALSE) | ||
write_csv(janda, "janda.csv", na = "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,20 @@ | ||
library(dplyr) | ||
library(readr) | ||
library(tidyverse) | ||
library(countrycode) | ||
|
||
marpor_raw <- read_csv("marpor-2016.csv") | ||
marpor <- marpor_raw %>% select(-country) | ||
|
||
marpor_share <- read_csv("marpor-share.csv") | ||
marpor <- marpor %>% left_join(marpor_share) | ||
|
||
marpor <- marpor_raw %>% select(-country) %>% left_join(marpor_share) | ||
|
||
# add Party Facts country codes | ||
marpor <- marpor %>% | ||
mutate(country = countrycode(countryname, 'country.name', 'iso3c', | ||
custom_match = c(`Northern Ireland`='NIR'))) | ||
mutate(country = countrycode(countryname, "country.name", "iso3c", | ||
custom_match = c(`Northern Ireland`="NIR"))) | ||
if(any(is.na(marpor$country))) { | ||
warning("Country name clean-up needed") | ||
} | ||
|
||
# replace party short longer than 25 chars | ||
marpor[nchar(marpor$abbrev) > 25 & ! is.na(marpor$abbrev), "abbrev"] <- NA | ||
|
||
write.csv(marpor, "marpor.csv", na="", row.names = FALSE, fileEncoding="utf-8") | ||
write_csv(marpor, "marpor.csv", na = "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.