Petra Lamborn 3 years ago
parent
commit
38d1c281fd
1 changed files with 35 additions and 0 deletions
  1. 35
    0
      votingpatterns.R

+ 35
- 0
votingpatterns.R View File

@@ -0,0 +1,35 @@
1
+library(dplyr)
2
+library(tidyr)
3
+dat <- read.csv("AdvancedVotesNewZealand.csv", stringsAsFactors = FALSE) %>%
4
+    mutate(Party = gsub(" $", "", Party),
5
+           Party = gsub("Mâori", "Māori", Party)) %>%
6
+    pivot_wider(id_cols = c(Year, Party), names_from = Type, values_from = c(Votes, Seats)) %>%
7
+    mutate(Votes_Other = Votes_Total - Votes_Advance,
8
+           Advance_Proportion = Votes_Other / Votes_Total,
9
+           Seat_Difference = Seats_Advance - Seats_Total)
10
+
11
+elec <- dat %>% group_by(Year) %>%
12
+    summarise(Total_Votes = sum(Votes_Total),
13
+              Total_Advance = sum(Votes_Advance),
14
+              Total_Other = sum(Votes_Other),
15
+              Total_Seats = sum(Seats_Total),
16
+              Advance_Seats = sum(Seats_Advance),
17
+              Seat_Differences = sum(abs(Seat_Difference)),
18
+              .groups = "drop") %>%
19
+    mutate(Prop_Advance = Total_Advance / Total_Other)
20
+
21
+partylev <- dat %>% left_join(elec, by = "Year") %>%
22
+    mutate(PV_Prop_Total = Votes_Total / Total_Votes,
23
+           PV_Prop_Advance = Votes_Advance / Total_Advance)
24
+
25
+partyav <- partylev %>% group_by(Party) %>%
26
+    summarise(Total_Mean_Prop = mean(PV_Prop_Total),
27
+              Advance_Mean_Prop = mean(PV_Prop_Advance),
28
+              .groups = "drop") %>% arrange(desc(Total_Mean_Prop))
29
+
30
+partylev %>% select(Year:Party, Votes_Advance, Votes_Other, Seats_Advance, Seats_Total, PV_Prop_Advance, PV_Prop_Total) %>%
31
+    pivot_longer(cols = Votes_Advance:PV_Prop_Total,
32
+                 names_to = c("Quantity", "Type"),
33
+                 names_pattern = "(.*)_([^_]*)$",
34
+                 values_to = "value") %>%
35
+    pivot_wider(names_from = "Quantity", values_from = "value") %>% drop_na()