This basic R script will:
Retrieve statewide, Tennessee-precinct-level November 2022 election results from the Tennessee Secretary of State’s web site’s “Historical Election Results” page, (https://sos.tn.gov/elections/results).
Filter the data for Rutherford County, Tennessee, and the precinct-level vote counts for Republican incumbent Bill Lee and Democratic challenger Jason Martin.
Calculate the total votes cast for either Lee or Martin and the percentage of those votes case for each candidate.
Compare the vote totals and indicate which candidate, Lee or Martin, won the precinct.
Sort the data in descending order by the percentage of votes for Lee.
Show the precinct-level data as an HTML table
Aggregate the precinct-level data to produce county-level data
Show the county-level data as an HTML table
if (!require("tidyverse"))
install.packages("tidyverse")
if (!require("gtExtras"))
install.packages("gtExtras")
library(tidyverse)
library(readxl)
library(gtExtras)
# Download and import election data
# from TN Secretary of State web site:
# https://sos.tn.gov/elections/results
download.file(
"https://sos-prod.tnsosgovfiles.com/s3fs-public/document/20221108AllbyPrecinct.xlsx",
"RawElectionData.xlsx",
quiet = TRUE,
mode = "wb")
RawElectionData <- read_xlsx("RawElectionData.xlsx", sheet = "SOFFICELso")
# Filter, calculate, and select
# to get data of interest
# then import results in a dataframe called MyData
MyData <- RawElectionData %>%
filter(COUNTY == "Rutherford",
CANDGROUP == "1") %>%
mutate(
Lee = PVTALLY1,
Martin = PVTALLY2,
Total = PVTALLY1 + PVTALLY2,
Lee_Pct = round(PVTALLY1 / (PVTALLY1 + PVTALLY2), 2),
Martin_Pct = round(PVTALLY2 / (PVTALLY1 + PVTALLY2), 2),
Winner = case_when(Lee > Martin ~ "Lee (R)",
Martin > Lee ~ "Martin (D)",
.default = "Tie")) %>%
arrange(desc(Lee)) %>%
select(COUNTY,
PRECINCT,
Total,
Lee,
Martin,
Lee_Pct,
Martin_Pct,
Winner)
# Show MyData as an HTML table
gt(MyData) %>%
tab_header("Precinct results") %>%
cols_align(align = "left") %>%
gt_theme_538
# Show county totals as an HTML table
MySummary <- MyData %>%
summarize(sum(Lee), sum(Martin), sum(Total)) %>%
rename("Lee" = "sum(Lee)",
"Martin" = "sum(Martin)",
"Total" = "sum(Total)") %>%
mutate(Pct_Lee = round(Lee / Total, digits = 2)) %>%
mutate(Pct_Martin = round(Martin / Total, digits = 2))
gt(MySummary) %>%
tab_header("County results") %>%
cols_align(align = "left") %>%
gt_theme_538
Precinct results | |||||||
COUNTY | PRECINCT | Total | Lee | Martin | Lee_Pct | Martin_Pct | Winner |
---|---|---|---|---|---|---|---|
Rutherford | 8-1 | 5013 | 3778 | 1235 | 0.75 | 0.25 | Lee (R) |
Rutherford | 9-1 | 5024 | 3425 | 1599 | 0.68 | 0.32 | Lee (R) |
Rutherford | 19-1 | 4644 | 3046 | 1598 | 0.66 | 0.34 | Lee (R) |
Rutherford | 14-2 | 4570 | 3002 | 1568 | 0.66 | 0.34 | Lee (R) |
Rutherford | 2-1 | 3397 | 2647 | 750 | 0.78 | 0.22 | Lee (R) |
Rutherford | 4-1 | 3727 | 2465 | 1262 | 0.66 | 0.34 | Lee (R) |
Rutherford | 7-1 | 3959 | 2416 | 1543 | 0.61 | 0.39 | Lee (R) |
Rutherford | 6-2 | 3357 | 2409 | 948 | 0.72 | 0.28 | Lee (R) |
Rutherford | 3-2 | 3341 | 2312 | 1029 | 0.69 | 0.31 | Lee (R) |
Rutherford | 13-1 | 3119 | 1836 | 1283 | 0.59 | 0.41 | Lee (R) |
Rutherford | 20-1 | 3084 | 1782 | 1302 | 0.58 | 0.42 | Lee (R) |
Rutherford | 15-1 | 2804 | 1716 | 1088 | 0.61 | 0.39 | Lee (R) |
Rutherford | 18-2 | 2142 | 1384 | 758 | 0.65 | 0.35 | Lee (R) |
Rutherford | 15-2 | 2111 | 1380 | 731 | 0.65 | 0.35 | Lee (R) |
Rutherford | 10-2 | 1952 | 1122 | 830 | 0.57 | 0.43 | Lee (R) |
Rutherford | 5-1 | 2042 | 1080 | 962 | 0.53 | 0.47 | Lee (R) |
Rutherford | 21-2 | 1951 | 1020 | 931 | 0.52 | 0.48 | Lee (R) |
Rutherford | 10-1 | 1772 | 997 | 775 | 0.56 | 0.44 | Lee (R) |
Rutherford | 12-1 | 1613 | 946 | 667 | 0.59 | 0.41 | Lee (R) |
Rutherford | 1-1 | 1911 | 909 | 1002 | 0.48 | 0.52 | Martin (D) |
Rutherford | 6-1 | 1082 | 840 | 242 | 0.78 | 0.22 | Lee (R) |
Rutherford | 11-2 | 1463 | 835 | 628 | 0.57 | 0.43 | Lee (R) |
Rutherford | 16-2 | 1397 | 813 | 584 | 0.58 | 0.42 | Lee (R) |
Rutherford | 2-2 | 1104 | 793 | 311 | 0.72 | 0.28 | Lee (R) |
Rutherford | 17-1 | 1504 | 705 | 799 | 0.47 | 0.53 | Martin (D) |
Rutherford | 11-1 | 1197 | 672 | 525 | 0.56 | 0.44 | Lee (R) |
Rutherford | 12-2 | 914 | 654 | 260 | 0.72 | 0.28 | Lee (R) |
Rutherford | 21-1 | 903 | 501 | 402 | 0.55 | 0.45 | Lee (R) |
Rutherford | 16-1 | 1017 | 406 | 611 | 0.40 | 0.60 | Martin (D) |
Rutherford | 18-1 | 680 | 207 | 473 | 0.30 | 0.70 | Martin (D) |
Rutherford | 14-1 | 338 | 187 | 151 | 0.55 | 0.45 | Lee (R) |
Rutherford | 3-1 | 192 | 157 | 35 | 0.82 | 0.18 | Lee (R) |
Rutherford | 4-2 | 199 | 133 | 66 | 0.67 | 0.33 | Lee (R) |
County results | ||||
Lee | Martin | Total | Pct_Lee | Pct_Martin |
---|---|---|---|---|
46575 | 26948 | 73523 | 0.63 | 0.37 |