Skip to content

Instantly share code, notes, and snippets.

@sysilviakim
Last active March 8, 2022 21:39
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/488fe80da664d7faaee0f0b4f0b4ccc9 to your computer and use it in GitHub Desktop.
Save sysilviakim/488fe80da664d7faaee0f0b4f0b4ccc9 to your computer and use it in GitHub Desktop.
Scrape Congressional Redistricting Authorities by State
library(dplyr)
library(tibble)
library(rvest)
library(xtable)
url_redist <- "https://ballotpedia.org/State-by-state_redistricting_procedures"
state_df <- read_html(url_redist) %>%
html_table(fill = TRUE) %>%
## Fourth table is the congressional redistricting table, after manual insp.
.[[4]] %>%
`colnames<-`(.[1, ]) %>%
.[-1, ] %>%
## Final column is empty
.[, -dim(.)[2]] %>%
## Final row is Source: ...
slice(-n()) %>%
## For clarity, delete state name from "state legislature"
rowwise() %>%
mutate(
`Who draws the lines?` = trimws(gsub(State, "", `Who draws the lines?`))
) %>%
## Abbreviate "Alaska is home to only one congressional district." pattern
mutate(
Notes = gsub(
paste0(State, " is home to only one congressional district."),
"Only one district.", Notes
)
) %>%
## In this case, to fit in Beamer, delete one-line districts...
filter(Notes != "Only one district.") ## %>%
## rename(`Gov. veto?` = `Can the governor veto the lines?`)
print(
xtable(state_df, digits = 0),
include.rownames = FALSE,
booktabs = TRUE
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment