Skip to content

Instantly share code, notes, and snippets.

@hdigital

hdigital/ppepe-partyfacts.R Secret

Created Jun 29, 2021
Embed
What would you like to do?
library(tidyverse)
# download and read Party Facts mapping table
file_name <- "partyfacts-mapping.csv"
if( ! file_name %in% list.files()) {
url <- "https://partyfacts.herokuapp.com/download/external-parties-csv/"
download.file(url, file_name)
}
partyfacts_raw <- read_csv(file_name, guess_max = 50000)
partyfacts <-
partyfacts_raw %>%
filter(! is.na(partyfacts_id)) %>%
select(country:name_english, partyfacts_id)
# link datasets (select only linked parties)
dataset_1 <- partyfacts %>% filter(dataset_key == "ppepe")
dataset_2 <- partyfacts %>% filter(dataset_key == "ches")
dataset_3 <- partyfacts %>% filter(dataset_key == "parlgov")
dataset_4 <- partyfacts %>% filter(dataset_key == "manifesto")
# Note: ParlGov import into Party Facts includes only parties with < 1% share in national parliament election
link_table <-
dataset_1 %>%
left_join(dataset_2, by = c("country" = "country", "partyfacts_id" = "partyfacts_id")) %>%
left_join(dataset_3, by = c("country" = "country", "partyfacts_id" = "partyfacts_id")) %>%
left_join(dataset_4, by = c("country" = "country", "partyfacts_id" = "partyfacts_id")) %>%
rename_with(~ str_replace(.x, fixed(".y.y"), "_manifesto")) %>%
rename_with(~ str_replace(.x, fixed(".x.x"), "_parlgov")) %>%
rename_with(~ str_replace(.x, fixed(".y"), "_ches")) %>%
rename_with(~ str_replace(.x, fixed(".x"), "_ppepe"))
# write results into file with dataset names in file name
file_out <- "partyfacts-linked.csv"
write_csv(link_table, file_out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment