Last active
October 9, 2023 16:44
-
-
Save k5cents/73962805b5ed91286bd7d61353336d06 to your computer and use it in GitHub Desktop.
Calculating the minimum number of votes needed to win the 2016 Electoral College
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(rvest) | |
# scrape state votes from wikipedia table | |
prez_wiki <- read_html("https://w.wiki/Zqi") | |
vote_table <- prez_wiki %>% | |
html_node(xpath = '//*[@id="mw-content-text"]/div[1]/div[40]/table') %>% | |
html_table(fill = TRUE) %>% | |
as_tibble(.name_repair = "unique") | |
vote_table <- vote_table %>% | |
# keep states, electoral college, votes | |
select(state = 1, ec1 = 4, ec2 = 7, votes = Totalvotes) %>% | |
slice(c(-1, -58, -59)) %>% # remove head and foot | |
na_if("–") %>% | |
unite(ec, starts_with("ec"), na.rm = TRUE) %>% | |
mutate(across(2:3, parse_number)) %>% | |
arrange(ec) %>% | |
mutate(min_win = floor(votes/2 + 1)) | |
write_csv(vote_table, "min-popvote.csv") | |
min_states <- min(which(cumsum(vote_table$ec) > 270)) # 46 smallest contests | |
min_votes <- sum(vote_table$min_win[1:min_states]) # 33,557,447 votes needed | |
min_votes/sum(vote_table$votes) # 24.27% of votes cast |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
† At-large state votes