Skip to content

Instantly share code, notes, and snippets.

@k5cents
Last active October 9, 2023 16:44
Show Gist options
  • Save k5cents/73962805b5ed91286bd7d61353336d06 to your computer and use it in GitHub Desktop.
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
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
@k5cents
Copy link
Author

k5cents commented Aug 18, 2020

State Electoral Votes Votes Cast Minimum Needed Total EC Total Votes Total Percent
ME-1 1 394,329 197,165 1.0 197,165 0.143%
ME-2 1 353,416 176,709 2.0 373,874 0.270%
NE-1 1 282,338 141,170 3.0 515,044 0.373%
NE-2 1 291,680 145,841 4.0 660,885 0.478%
NE-3 1 270,109 135,055 5.0 795,940 0.576%
Maine † 2 747,927 373,964 7.0 1,169,904 0.846%
Nebr. † 2 844,227 422,114 9.0 1,592,018 1.151%
Alaska 3 318,608 159,305 12.0 1,751,323 1.267%
Del. 3 443,814 221,908 15.0 1,973,231 1.427%
D.C. 3 311,268 155,635 18.0 2,128,866 1.540%
Hawaii 3 428,937 214,469 21.0 2,343,335 1.695%
Mont. 3 497,147 248,574 24.0 2,591,909 1.875%
N.D. 3 344,360 172,181 27.0 2,764,090 1.999%
S.D. 3 370,093 185,047 30.0 2,949,137 2.133%
Vt. 3 315,067 157,534 33.0 3,106,671 2.247%
Wyo. 3 255,849 127,925 36.0 3,234,596 2.339%
Idaho 4 690,255 345,128 40.0 3,579,724 2.589%
N.H. 4 744,296 372,149 44.0 3,951,873 2.858%
R.I. 4 464,144 232,073 48.0 4,183,946 3.026%
N.M. 5 798,319 399,160 53.0 4,583,106 3.315%
W.Va. 5 714,423 357,212 58.0 4,940,318 3.573%
Ark. 6 1,130,635 565,318 64.0 5,505,636 3.982%
Iowa 6 1,566,031 783,016 70.0 6,288,652 4.548%
Kan. 6 1,184,402 592,202 76.0 6,880,854 4.977%
Miss. 6 1,209,357 604,679 82.0 7,485,533 5.414%
Nev. 6 1,125,385 562,693 88.0 8,048,226 5.821%
Utah 6 1,131,430 565,716 94.0 8,613,942 6.230%
Conn. 7 1,644,920 822,461 101.0 9,436,403 6.825%
Okla. 7 1,452,992 726,497 108.0 10,162,900 7.351%
Ore. 7 2,001,336 1,000,669 115.0 11,163,569 8.074%
Ky. 8 1,924,149 962,075 123.0 12,125,644 8.770%
La. 8 2,029,032 1,014,517 131.0 13,140,161 9.504%
Wash. 8 3,317,019 1,658,510 139.0 14,798,671 10.703%
Ala. 9 2,123,372 1,061,687 148.0 15,860,358 11.471%
Colo. 9 2,780,247 1,390,124 157.0 17,250,482 12.477%
S.C. 9 2,103,027 1,051,514 166.0 18,301,996 13.237%
Md. 10 2,781,446 1,390,724 176.0 19,692,720 14.243%
Minn. 10 2,944,813 1,472,407 186.0 21,165,127 15.308%
Mo. 10 2,808,605 1,404,303 196.0 22,569,430 16.324%
Wis. 10 2,976,150 1,488,076 206.0 24,057,506 17.400%
Ariz. 11 2,573,165 1,286,583 217.0 25,344,089 18.331%
Ind. 11 2,734,958 1,367,480 228.0 26,711,569 19.320%
Mass. 11 3,325,046 1,662,524 239.0 28,374,093 20.522%
Tenn. 11 2,508,027 1,254,014 250.0 29,628,107 21.429%
Va. 13 3,984,631 1,992,316 263.0 31,620,423 22.870%
N.J. 14 3,874,046 1,937,024 277.0 33,557,447 24.271%
N.C. 15 4,741,564 2,370,783 292.0 35,928,230 25.986%
Ga. 16 4,114,732 2,057,367 308.0 37,985,597 27.474%
Mich. 16 4,799,284 2,399,643 324.0 40,385,240 29.209%
Ohio 18 5,496,487 2,748,244 342.0 43,133,484 31.197%
Ill. 20 5,536,424 2,768,213 362.0 45,901,697 33.199%
Pa. 20 6,165,478 3,082,740 382.0 48,984,437 35.429%
Fla. 29 9,420,039 4,710,020 411.0 53,694,457 38.836%
N.Y. 29 7,721,453 3,860,727 440.0 57,555,184 41.628%
Texas 36 8,969,226 4,484,614 476.0 62,039,798 44.871%
Calif. 55 14,181,595 7,090,798 531.0 69,130,596 50.000%

† At-large state votes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment