Created
November 2, 2021 19:43
-
-
Save sysilviakim/08ed4ad240195c39fdfbd6b6ed135d41 to your computer and use it in GitHub Desktop.
Calculate average turnout of swing vs. non-swing states
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(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