---
title: "House of Representatives - Get the data"
resource_files:
  - vignetttes/house-preference-flow*.png
author: "Carlos YANEZ SANTIBANEZ"
date: "2022-11-28"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{House of Representatives - Get the data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---



Although this package contains many convenient functions to process and displays the election data, there are probably many cases where the data in its original format is preferable - especially by an advanced analysis doing ad-hoc analysis. For these reasons, the functions that read the data in its "original" form are also exported for use. These are:

- get_house_primary_vote()
- get_house_preferences()
- get_house_2PF()
- get_house_2PP()
- get_house_MPs()
- get_house_turnout()

Please note that the presented here has been downloaded from the AEC's website.

## Primary Vote

It is possible to get all primary vote results for all electorates in all elections from 2044, by running *get_house_primary_vote()*. More usefully, the function allows filtering by a number of electorates for a number of years, as shown in the below example:


```r
get_house_primary_vote(division = c("Cowan","Curtin"),year=c(2019,2022))
#> # A tibble: 1,735 × 17
#>     Year StateAb DivisionID DivisionNm PollingPlaceID PollingPlace      CandidateID Surname     GivenNm     Ballo…¹ Elected Histo…² PartyAb PartyNm Ordin…³ Swing Sitti…⁴
#>    <dbl> <chr>        <int> <chr>               <int> <chr>                   <int> <chr>       <chr>         <int> <lgl>   <lgl>   <chr>   <chr>     <int> <dbl> <lgl>  
#>  1  2022 WA             237 Cowan               11591 Alexander Heights       37205 ALY         Anne              1 TRUE    TRUE    ALP     Austra…     688  9.18 FALSE  
#>  2  2022 WA             237 Cowan               11591 Alexander Heights       36591 TRIPP       Isabella          2 FALSE   FALSE   GRN     The Gr…     124 -1.47 FALSE  
#>  3  2022 WA             237 Cowan               11591 Alexander Heights       36484 IRADUKUNDA  Sylvia            3 FALSE   FALSE   AUC     Austra…      28  0.05 FALSE  
#>  4  2022 WA             237 Cowan               11591 Alexander Heights       37243 CALAUTTI    Michael           4 FALSE   FALSE   CYA     Austra…      27  1.96 FALSE  
#>  5  2022 WA             237 Cowan               11591 Alexander Heights       37387 WALSH       Tyler             5 FALSE   FALSE   ON      Paulin…      38 -3.35 FALSE  
#>  6  2022 WA             237 Cowan               11591 Alexander Heights       37222 van KRIEKEN Micah             6 FALSE   FALSE   LDP     Libera…      11  0.8  FALSE  
#>  7  2022 WA             237 Cowan               11591 Alexander Heights       37841 LAVERACK    Roland            7 FALSE   FALSE   WAP     WESTER…      26  1.89 FALSE  
#>  8  2022 WA             237 Cowan               11591 Alexander Heights       36483 HAND        Claire Eli…       8 FALSE   FALSE   UAPP    United…      34 -0.24 FALSE  
#>  9  2022 WA             237 Cowan               11591 Alexander Heights       36574 CONNELLY    Vince             9 FALSE   TRUE    LP      Liberal     369 -9.96 FALSE  
#> 10  2022 WA             237 Cowan               11591 Alexander Heights       37219 ANAGNO      Michael          10 FALSE   FALSE   AJP     Animal…      33  2.39 FALSE  
#> # … with 1,725 more rows, and abbreviated variable names ¹​BallotPosition, ²​HistoricElected, ³​OrdinaryVotes, ⁴​SittingMemberFl
```

The function allows also it to narrow down to specific electorates. For instance, to check the primary vote for ALP candidates, for both electorates in polling places named "Perth":


```r
library(dplyr)

get_house_primary_vote(division = c("Cowan","Curtin"),
                       year=c(2019,2022),
                       polling_places = "Perth") |>
  filter(PartyAb=="ALP")
#> # A tibble: 4 × 17
#>    Year StateAb DivisionID DivisionNm PollingPlaceID PollingPlace               CandidateID Surname GivenNm Ballo…¹ Elected Histo…² PartyAb PartyNm Ordin…³ Swing Sitti…⁴
#>   <dbl> <chr>        <int> <chr>               <int> <chr>                            <int> <chr>   <chr>     <int> <lgl>   <lgl>   <chr>   <chr>     <int> <dbl> <lgl>  
#> 1  2022 WA             237 Cowan              107098 Perth CBD East COWAN PPVC        37205 ALY     Anne          1 TRUE    TRUE    ALP     Austra…      97 15.0  FALSE  
#> 2  2022 WA             238 Curtin              99650 Perth CBD East CURTIN PPVC       37282 SPENCER Yannick       7 FALSE   FALSE   ALP     Austra…      50 -0.88 FALSE  
#> 3  2022 WA             238 Curtin              94024 Perth CBD West CURTIN PPVC       37282 SPENCER Yannick       7 FALSE   FALSE   ALP     Austra…     168 -2.5  FALSE  
#> 4  2019 WA             238 Curtin              94024 Perth CURTIN PPVC                33431 MEECHAM Rob           7 FALSE   FALSE   ALP     Austra…     316  0.43 FALSE  
#> # … with abbreviated variable names ¹​BallotPosition, ²​HistoricElected, ³​OrdinaryVotes, ⁴​SittingMemberFl
```

The function also can present the division totals, for example:


```r
get_house_primary_vote(division = c("Cowan","Curtin"),
                       year=c(2019,2022), 
                       aggregation = TRUE)
#> # A tibble: 37 × 14
#>     Year StateAb DivisionID DivisionNm CandidateID Surname  GivenNm    BallotPosition Elected HistoricElected PartyAb  PartyNm                       SittingMem…¹ Ordin…²
#>    <dbl> <chr>        <int> <chr>            <int> <chr>    <chr>               <int> <lgl>   <lgl>           <chr>    <chr>                         <lgl>          <int>
#>  1  2019 WA             237 Cowan              999 Informal Informal              999 FALSE   FALSE           Informal Informal                      FALSE           4090
#>  2  2019 WA             237 Cowan            32239 COOPER   Mark                    1 FALSE   FALSE           GRN      The Greens (WA)               FALSE           6493
#>  3  2019 WA             237 Cowan            32778 LEBRASSE Andre                   4 FALSE   FALSE           AUC      Australian Christians         FALSE           1514
#>  4  2019 WA             237 Cowan            32875 BEDFORD  Paul James              6 FALSE   FALSE           ASP      Shooters, Fishers and Farmers FALSE           1101
#>  5  2019 WA             237 Cowan            32934 WESTCOTT Peter                   3 FALSE   FALSE           UAPP     United Australia Party        FALSE           1582
#>  6  2019 WA             237 Cowan            33348 ALY      Anne                    2 TRUE    TRUE            ALP      Australian Labor Party        FALSE          26330
#>  7  2019 WA             237 Cowan            33389 STEWART  Isaac                   7 FALSE   FALSE           LP       Liberal                       FALSE          26721
#>  8  2019 WA             237 Cowan            33450 MUNDY    Sheila                  5 FALSE   FALSE           ON       Pauline Hanson's One Nation   FALSE           3641
#>  9  2019 WA             238 Curtin             999 Informal Informal              999 FALSE   FALSE           Informal Informal                      FALSE           2364
#> 10  2019 WA             238 Curtin           32149 PIDGEON  Cameron                 2 FALSE   FALSE           GRN      The Greens (WA)               FALSE          10905
#> # … with 27 more rows, and abbreviated variable names ¹​SittingMemberFl, ²​OrdinaryVotes
```

## Preferences

To retrieve preferences for one division for a particular year, the function is called *get_house_preferences()*


```r
get_house_preferences("Wills",2019) |> head(10)
#> # A tibble: 10 × 16
#>     Year StateAb DivisionId DivisionNm  PPId PPNm   CountNum BallotPosition CandidateId Surname GivenNm           PartyAb PartyNm                 Sitti…¹ Calcu…² Calcu…³
#>    <dbl> <chr>        <int> <chr>      <int> <chr>     <int>          <int>       <int> <chr>   <chr>             <chr>   <chr>                   <chr>   <chr>     <dbl>
#>  1  2019 VIC            234 Wills          0 ABSENT        0              1       32688 MILES   Christopher James AJP     Animal Justice Party    N       Prefer…  254   
#>  2  2019 VIC            234 Wills          0 ABSENT        0              1       32688 MILES   Christopher James AJP     Animal Justice Party    N       Prefer…    4.65
#>  3  2019 VIC            234 Wills          0 ABSENT        0              1       32688 MILES   Christopher James AJP     Animal Justice Party    N       Transf…    0   
#>  4  2019 VIC            234 Wills          0 ABSENT        0              1       32688 MILES   Christopher James AJP     Animal Justice Party    N       Transf…    0   
#>  5  2019 VIC            234 Wills          0 ABSENT        0              2       32398 BOLTON  Susanne Alison    VNS     Victorian Socialists    N       Prefer…  238   
#>  6  2019 VIC            234 Wills          0 ABSENT        0              2       32398 BOLTON  Susanne Alison    VNS     Victorian Socialists    N       Prefer…    4.35
#>  7  2019 VIC            234 Wills          0 ABSENT        0              2       32398 BOLTON  Susanne Alison    VNS     Victorian Socialists    N       Transf…    0   
#>  8  2019 VIC            234 Wills          0 ABSENT        0              2       32398 BOLTON  Susanne Alison    VNS     Victorian Socialists    N       Transf…    0   
#>  9  2019 VIC            234 Wills          0 ABSENT        0              3       33251 KILLIN  Peter John        LP      Liberal Party of Austr… N       Prefer…  862   
#> 10  2019 VIC            234 Wills          0 ABSENT        0              3       33251 KILLIN  Peter John        LP      Liberal Party of Austr… N       Prefer…   15.8 
#> # … with abbreviated variable names ¹​SittingMemberFl, ²​CalculationType, ³​CalculationValue
```

Similarly to the primary vote function, results can be aggregated and filtered by polling place:


```r
get_house_preferences("Wills",2019,
                      aggregation = TRUE)
#> # A tibble: 60 × 13
#>     Year StateAb DivisionId DivisionNm CountNum BallotPosition CandidateId Surname GivenNm           PartyAb PartyNm                                      Calcu…¹ Calcu…²
#>    <dbl> <chr>        <int> <chr>         <int>          <int>       <int> <chr>   <chr>             <chr>   <chr>                                        <chr>     <dbl>
#>  1  2019 VIC            234 Wills             0              1       32688 MILES   Christopher James AJP     Animal Justice Party                         Prefer…    3596
#>  2  2019 VIC            234 Wills             0              1       32688 MILES   Christopher James AJP     Animal Justice Party                         Transf…       0
#>  3  2019 VIC            234 Wills             0              2       32398 BOLTON  Susanne Alison    VNS     Victorian Socialists                         Prefer…    4344
#>  4  2019 VIC            234 Wills             0              2       32398 BOLTON  Susanne Alison    VNS     Victorian Socialists                         Transf…       0
#>  5  2019 VIC            234 Wills             0              3       33251 KILLIN  Peter John        LP      Liberal Party of Australia (Victorian Divis… Prefer…   17241
#>  6  2019 VIC            234 Wills             0              3       33251 KILLIN  Peter John        LP      Liberal Party of Australia (Victorian Divis… Transf…       0
#>  7  2019 VIC            234 Wills             0              4       32657 PULFORD Adam Ross         GVIC    The Australian Greens - Victoria             Prefer…   25575
#>  8  2019 VIC            234 Wills             0              4       32657 PULFORD Adam Ross         GVIC    The Australian Greens - Victoria             Transf…       0
#>  9  2019 VIC            234 Wills             0              5       32329 VENKAT  Manju             UAPP    United Australia Party                       Prefer…    2979
#> 10  2019 VIC            234 Wills             0              5       32329 VENKAT  Manju             UAPP    United Australia Party                       Transf…       0
#> # … with 50 more rows, and abbreviated variable names ¹​CalculationType, ²​CalculationValue
```


```r
get_house_preferences("Wills",2019, polling_places=c("ABSENT")) |> 
  head(10)
#> # A tibble: 10 × 16
#>     Year StateAb DivisionId DivisionNm  PPId PPNm   CountNum BallotPosition CandidateId Surname GivenNm           PartyAb PartyNm                 Sitti…¹ Calcu…² Calcu…³
#>    <dbl> <chr>        <int> <chr>      <int> <chr>     <int>          <int>       <int> <chr>   <chr>             <chr>   <chr>                   <chr>   <chr>     <dbl>
#>  1  2019 VIC            234 Wills          0 ABSENT        0              1       32688 MILES   Christopher James AJP     Animal Justice Party    N       Prefer…  254   
#>  2  2019 VIC            234 Wills          0 ABSENT        0              1       32688 MILES   Christopher James AJP     Animal Justice Party    N       Prefer…    4.65
#>  3  2019 VIC            234 Wills          0 ABSENT        0              1       32688 MILES   Christopher James AJP     Animal Justice Party    N       Transf…    0   
#>  4  2019 VIC            234 Wills          0 ABSENT        0              1       32688 MILES   Christopher James AJP     Animal Justice Party    N       Transf…    0   
#>  5  2019 VIC            234 Wills          0 ABSENT        0              2       32398 BOLTON  Susanne Alison    VNS     Victorian Socialists    N       Prefer…  238   
#>  6  2019 VIC            234 Wills          0 ABSENT        0              2       32398 BOLTON  Susanne Alison    VNS     Victorian Socialists    N       Prefer…    4.35
#>  7  2019 VIC            234 Wills          0 ABSENT        0              2       32398 BOLTON  Susanne Alison    VNS     Victorian Socialists    N       Transf…    0   
#>  8  2019 VIC            234 Wills          0 ABSENT        0              2       32398 BOLTON  Susanne Alison    VNS     Victorian Socialists    N       Transf…    0   
#>  9  2019 VIC            234 Wills          0 ABSENT        0              3       33251 KILLIN  Peter John        LP      Liberal Party of Austr… N       Prefer…  862   
#> 10  2019 VIC            234 Wills          0 ABSENT        0              3       33251 KILLIN  Peter John        LP      Liberal Party of Austr… N       Prefer…   15.8 
#> # … with abbreviated variable names ¹​SittingMemberFl, ²​CalculationType, ³​CalculationValue
```
The AEC also lists how the primary votes have flowed to the two finalists. They can be retrieved with *get_house_2PF*. Being related to the previous function, it also has the same parameters.


```r
get_house_2PF(division="Jagajaga",year=2013,aggregation = TRUE)
#> # A tibble: 8 × 17
#>    Year StateAb DivisionId DivisionNm FromCandidateId FromCandidatePartyAb FromCandidat…¹ FromC…² FromC…³ FromC…⁴ ToCan…⁵ ToCan…⁶ ToCan…⁷ ToCan…⁸ ToCan…⁹ ToCan…˟ Trans…˟
#>   <dbl> <chr>        <int> <chr>                <int> <chr>                <chr>          <chr>   <chr>     <int>   <int> <chr>   <chr>   <chr>   <chr>     <int>   <int>
#> 1  2013 VIC            220 Jagajaga             28114 GRN                  Australian Gr… McKINN… Hugh          4   28255 ALP     Austra… MACKLIN Jenny         1   12016
#> 2  2013 VIC            220 Jagajaga             28114 GRN                  Australian Gr… McKINN… Hugh          4   28892 LP      Libera… MULHOL… David         3    1680
#> 3  2013 VIC            220 Jagajaga             28175 AJP                  Animal Justic… SCHRAM  Nathan…       5   28255 ALP     Austra… MACKLIN Jenny         1    1383
#> 4  2013 VIC            220 Jagajaga             28175 AJP                  Animal Justic… SCHRAM  Nathan…       5   28892 LP      Libera… MULHOL… David         3     852
#> 5  2013 VIC            220 Jagajaga             28255 ALP                  Australian La… MACKLIN Jenny         1   28255 ALP     Austra… MACKLIN Jenny         1   36238
#> 6  2013 VIC            220 Jagajaga             28885 FFP                  Family First … WARD    Jessica       2   28255 ALP     Austra… MACKLIN Jenny         1     899
#> 7  2013 VIC            220 Jagajaga             28885 FFP                  Family First … WARD    Jessica       2   28892 LP      Libera… MULHOL… David         3    1442
#> 8  2013 VIC            220 Jagajaga             28892 LP                   Liberal Party… MULHOL… David         3   28892 LP      Libera… MULHOL… David         3   37920
#> # … with abbreviated variable names ¹​FromCandidatePartyNm, ²​FromCandidateSurname, ³​FromCandidateGivenNm, ⁴​FromCandidateBallotPosition, ⁵​ToCandidateId,
#> #   ⁶​ToCandidatePartyAb, ⁷​ToCandidatePartyNm, ⁸​ToCandidateSurname, ⁹​ToCandidateGivenNm, ˟​ToCandidateBallotPosition, ˟​TransferCount
```
The last preference function is *get_house_2PP()*, which provides the [Two-Party-Preferred](https://en.wikipedia.org/wiki/Two-party-preferred_vote) summaries as recorded by the AEC (usually, Coalition vs. ALP.)


```r
get_house_2PP(division = "Indi",
              year=2016,
              aggregation = TRUE)
#> # A tibble: 1 × 9
#>    Year StateAb DivisionID DivisionNm `Liberal/National Coalition Votes` `Australian Labor Party Votes` TotalVotes `Liberal/National Coalition Percentage` Australian L…¹
#>   <dbl> <chr>        <int> <chr>                                   <int>                          <int>      <int>                                   <dbl>          <dbl>
#> 1  2016 VIC            218 Indi                                    41083                          34375      75458                                   0.544          0.456
#> # … with abbreviated variable name ¹​`Australian Labor Party Percentage`
```



## Elected MPs

The function *get_house_MPs()* retrieves the list of elected candidates. Without parameters, it returns the complete lists (all divisions, for all elections), but similarly to the previous functions it allows for results to be filtered by divisions and years


```r
get_house_MPs(division = c("Melbourne","Cooper"),
              year = c(2019,2022))
#> # A tibble: 4 × 9
#>    Year DivisionID DivisionNm StateAb CandidateID GivenNm Surname PartyNm                PartyAb
#>   <dbl>      <int> <chr>      <chr>         <int> <chr>   <chr>   <chr>                  <chr>  
#> 1  2022        320 Cooper     VIC           36384 Ged     KEARNEY Australian Labor Party ALP    
#> 2  2022        228 Melbourne  VIC           36551 Adam    BANDT   The Greens             GRN    
#> 3  2019        320 Cooper     VIC           32339 Ged     KEARNEY Australian Labor Party ALP    
#> 4  2019        228 Melbourne  VIC           32423 Adam    BANDT   The Greens             GRN
```

## Turnout

The function *get_house_turnout()* retrieves turnout data as published by the AEC. It can be filtered by division and year


```r
get_house_turnout(year=2019)
#> # A tibble: 151 × 8
#>     Year DivisionID DivisionNm StateAb Enrolment Turnout TurnoutPercentage TurnoutSwing
#>    <dbl>      <int> <chr>      <chr>       <int>   <int>             <dbl>        <dbl>
#>  1  2019        179 Adelaide   SA         121606  111299              91.5         1.73
#>  2  2019        197 Aston      VIC        110342  103919              94.2        -0.69
#>  3  2019        198 Ballarat   VIC        114954  107372              93.4         1.03
#>  4  2019        103 Banks      NSW        106253   98845              93.0         1.04
#>  5  2019        180 Barker     SA         118371  111893              94.5         0.5 
#>  6  2019        104 Barton     NSW        108992   99380              91.2         1.34
#>  7  2019        192 Bass       TAS         76532   71973              94.0         0.19
#>  8  2019        318 Bean       ACT        104482   97982              93.8         0.09
#>  9  2019        200 Bendigo    VIC        112755  105979              94.0         0.51
#> 10  2019        105 Bennelong  NSW        108815  101542              93.3         1.61
#> # … with 141 more rows
```
