instant_runoff_voting: Instant run-off voting

View source: R/specific_calculations.R

instant_runoff_votingR Documentation

Instant run-off voting

Description

This is a recursive function and may take some time to run.

Usage

instant_runoff_voting(df)

Arguments

df

(Dataframe) A dataframe that must contain these three columns:

voter

Unique ID for each voter.

rank

The rank preference that voter assigned to candidate.

candidate

A candidate they voted for. For rigorous results, candidate should be an ordered factor, with order randomly assigned to candidates (or just give each candidate a random number and use that as their ID). Without this, if two candidates get the same proportion of preferences, then alphabetical sorting will push the candidate whose name comes later in the alphabet to the bottom of the table, where they will systematically be removed first.

The dataframe should be in long format: each voter should have a new row for each of their rank and candidate votes. Each voter can have a different number of rows if they ranked a different number of candidates.

For an example, see the nz_boty_2019_head dataset in this package.

Details

It calculates the results of an instant run-off voting election where each voter assigns a rank preference to a candidate (1 == most preferred, 2 == second-most, etc.). Everyone's first-preferences are counted, and if no candidate gets >50% of the first-preferences, the candidate who got the fewest preferences is removed, the people who voted for them have their votes passed down to their next-preferred candidate, and the process begins again.

Value

A dataframe showing the winners and the proportion of votes they got.

Authors

Examples

head(nz_boty_2019_head)

#> # A tibble: 6 × 3
#>   voter  rank candidate          
#>   <int> <int> <chr>              
#> 1     1     1 Gibson's Albatross 
#> 2     1     2 Tūī                
#> 3     1     3 Kākā               
#> 4     1     4 Kākāpō             
#> 5     1     5 Little Spotted Kiwi
#> 6     2     1 Spotted Shag  

instant_runoff_voting(nz_boty_2019_head)

#>           candidate      prop
#> Yellow-eyed penguin 0.5009901
#>              Kākāpō 0.4990099



DesiQuintans/desiderata documentation built on April 9, 2023, 5:43 a.m.