-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path06-parlgov-cabinets.R
69 lines (51 loc) · 1.67 KB
/
06-parlgov-cabinets.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Title: Create ParlGov data set for government/opposition voting example
# Description:
# - remove caretaker cabinets
# - keep only cabinet formed after election
# - add ESS party IDs
library(conflicted)
library(tidyverse)
conflicts_prefer(dplyr::filter, .quiet = TRUE)
library(glue)
## Read data sources ----
parlgov_raw <- read_csv("data-raw/parlgov_view_cabinet.csv")
pf_links_raw <- read_rds("data/03-party-facts-links.rds")
## Context data ----
cab_96 <-
parlgov_raw |>
filter(election_date >= "1996-01-01") |>
rename(country = country_name_short)
countries <-
pf_links_raw |>
select(party_id = parlgov_id) |>
inner_join(select(cab_96, country, party_id), multiple = "first") |>
distinct(country)
cabinet_first <-
cab_96 |>
filter(caretaker == 0) |>
slice(1, .by = c(country, election_date)) |>
select(country, election_date, start_date)
## ParlGov cabinets with ESS IDs ----
pf_ess_parlgov <-
pf_links_raw |>
distinct(first_ess_id, parlgov_id) |>
mutate(cntry = substr(first_ess_id, 1, 2)) |>
rename(party_id = parlgov_id) |>
na.omit()
# keep necessary many-to-many relations (use filters DEU and LTU)
parlgov <-
cab_96 |>
inner_join(countries) |>
inner_join(cabinet_first, ) |>
left_join(pf_ess_parlgov, relationship = "many-to-many") |>
filter(!str_detect(first_ess_id, "lt2|lt3|de1"))
merge_check <-
parlgov |>
mutate(dup = n(), .by = c(country, election_date, party_name)) |>
filter(dup >= 2) |>
select(country, start_date, cabinet_name, party_name_short, first_ess_id)
if (nrow(merge_check >= 2)) {
warning("Multiple matches for some cabinet parties")
merge_check
}
write_rds(parlgov, "data/06-parlgov-cabinets.rds")