Introduction

aec_read <- function(url_df) {
        mapping <- tibble(ID = c("24310", "27966"),
                      Year = c(2019, 2022))
        
        result <- read_csv(url_df,
                     skip = 1,
                     id = "ID",
                     show_col_types = FALSE) %>%
        mutate(ID = str_extract(ID, "[0-9]{5}"),
               across(starts_with("PartyAb"), as_factor)) %>%
        inner_join(mapping, by = "ID") %>%
        select(-ID)
        
        return(result)
}
tcp_url <- c(
        "https://results.aec.gov.au/24310/Website/Downloads/HouseTcpByCandidateByVoteTypeDownload-24310.csv",
        "https://tallyroom.aec.gov.au/Downloads/HouseTcpByCandidateByVoteTypeDownload-27966.csv"
        )


comb_tcp <- aec_read(tcp_url)
nc_url <- c("https://tallyroom.aec.gov.au/Downloads/HouseNonClassicDivisionsDownload-27966.csv",
        "https://results.aec.gov.au/24310/Website/Downloads/HouseNonClassicDivisionsDownload-24310.csv")


non_classic <- aec_read(nc_url)

Two Candiate Prefered Vote

tcp_long <- comb_tcp %>%
        group_by(Year, PartyAb) %>%
        summarise(PartyVote = sum(TotalVotes)) %>%
        summarise(PartyAb = PartyAb,
                  PartyVote = PartyVote,
                  TCP = round(100 * PartyVote / sum(PartyVote), 2))

tcp_summary <- tcp_long %>%
        pivot_wider(names_from = Year,
                    values_from = PartyVote:TCP,
                    values_fill = 0) %>%
        mutate(Swing = TCP_2022 - TCP_2019) %>%
        arrange(desc(TCP_2022))


knitr::kable(tcp_summary,
             col.names = c("Party", "TC Vote 2019", "TC Vote 2022",
                           "TCP 2019", "TCP 2022", "TCP Swing"),
             caption = "Two Candidate Vote - All Divisions"
             )
Two Candidate Vote - All Divisions
Party TC Vote 2019 TC Vote 2022 TCP 2019 TCP 2022 TCP Swing
ALP 6414107 6476333 45.00 45.52 0.52
LP 4708843 4202555 33.04 29.54 -3.50
LNP 1624937 1550958 11.40 10.90 -0.50
IND 315239 721100 2.21 5.07 2.86
NP 779313 713303 5.47 5.01 -0.46
GRN 217663 387247 1.53 2.72 1.19
XEN 62124 71495 0.44 0.50 0.06
KAP 58231 59028 0.41 0.41 0.00
CLP 47415 44767 0.33 0.31 -0.02
ON 25521 0 0.18 0.00 -0.18

Two Candidate Prefered - Classic Divisions

classic_tcp <- comb_tcp %>%
        anti_join(non_classic,
                  by = c("Year", "DivisionNm")) %>%
        mutate(PartyAb = as_factor(PartyAb),
               PartyAb = fct_collapse(PartyAb,
                                      LNC = c("LP", "NP", "LNP", "CLP")))

ctcp <- classic_tcp %>%
        group_by(Year, PartyAb) %>%
        summarise(PartyVote = sum(TotalVotes)) %>%
        mutate(PartyPC = round(PartyVote / sum(PartyVote) * 100, 2))

ctcp_summary <- ctcp %>%
        select(-PartyVote) %>%
        pivot_wider(names_from = Year,
                    values_from = PartyPC,
                    values_fill = 0) %>%
        mutate(Swing = `2022` - `2019`)
        

knitr::kable(ctcp_summary, 
             col.names = c("Party", "2019", "2022", "Swing"),
             caption = "Classic Two Party Prefered")
Classic Two Party Prefered
Party 2019 2022 Swing
LNC 51.55 48.2 -3.35
ALP 48.45 51.8 3.35