Skip to content

Instantly share code, notes, and snippets.

@sysilviakim
Created November 2, 2021 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sysilviakim/08ed4ad240195c39fdfbd6b6ed135d41 to your computer and use it in GitHub Desktop.
Save sysilviakim/08ed4ad240195c39fdfbd6b6ed135d41 to your computer and use it in GitHub Desktop.
Calculate average turnout of swing vs. non-swing states
library(tidyverse)
library(readxl)
library(janitor)
download.file(
url =
"https://www2.census.gov/programs-surveys/cps/tables/p20/585/table04a.xlsx",
destfile = "turnout2020.xlsx",
mode = "wb"
)
turnout2020 <- read_excel("turnout2020.xlsx", skip = 5) %>%
clean_names() %>%
rename(state = x1) %>%
filter(state != "UNITED STATES") %>%
mutate(state = tolower(state)) %>%
left_join(
., data.frame(state = state.name, abb = state.abb) %>%
mutate(state = tolower(state)),
by = "state"
) %>%
mutate(
swing = case_when(
abb %in% c("AZ", "FL", "GA", "MI", "MN", "NC", "PA", "WI") ~ 1,
TRUE ~ 0
)
)
turnout2020 %>%
filter(!is.na(swing) & !is.na(percent_registered_total)) %>%
group_by(swing) %>%
summarize(mean(percent_registered_total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment