-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a version of datadoc_cd-info and create_dist-level-data for 2022 and 2024 data
- Loading branch information
1 parent
6052f73
commit b26d4df
Showing
7 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#' Congressional District level information by The Downballot (formerly Daily Kos) | ||
#' | ||
#' | ||
#' Some of the most consequential variables to include in MRP are at the | ||
#' district-level. We include one such data for congressional districts. All data | ||
#' is collected by The Downballot. `cd_info_2022` is data on 2022 boundaries; `cd_info_2024` | ||
#' uses 2024 boundaries. | ||
#' | ||
#' @format Both `cd_info_2022` and `cd_info_2024` are dataframes with all `r nrow(cd_info_2022)` Congressional | ||
#' Districts, one row per cd. | ||
#' \describe{ | ||
#' \item{year}{The year for the district line. A congressional district's | ||
#' actual geography can change year to year, and significantly so in different | ||
#' redistricting cycles. Lines try to get the contemporaneous district map, | ||
#' so that cd_info_2022 uses 2022 maps and cd_info_2024 uses 2024 maps.} | ||
#' \item{cd}{District code. The formatting corresponds to the CCES cumulative | ||
#' coding of \code{cd}: a two-letter abbreviation for the state followed by | ||
#' a dash, and the district number padded with zeros to the left to be of length | ||
#' 2. At-large districts like Alaska are given a "-AL" for the district number.} | ||
#' \item{presvotes_total}{In presidential years, the total number of votes cast for | ||
#' the office of President that year. Taken from the Downballot estimates from precinct results.} | ||
#' \item{pct_trump}{The two-party voteshare of Donald Trump in that district in 2020.} | ||
#' \item{downballot_name}{The unique descriptive name for the district code in | ||
#' 2018 given by The Downballot. Some edits are made for changing district. See | ||
#' Source for full citation.} | ||
#' \item{largest_place}{The largest place in the district code in 2020 given by The Downballot. Multiple districts may | ||
#' have the same largest place.} | ||
#' } | ||
#' | ||
#' | ||
#' @source | ||
#' The Downballot (formerly Daily Kos Elections), \url{https://www.the-downballot.com/p/data} | ||
#' | ||
#' @source | ||
#' The Daily Kos Elections naming guide to the nation's congressional districts. | ||
#' \url{https://bit.ly/2XsFI5W} | ||
#' | ||
#' The Downballot, "Daily Kos Elections 2020 presidential election results for congressional districts used in 2022 elections" | ||
#' \url{https://docs.google.com/spreadsheets/d/1CKngqOp8fzU22JOlypoxNsxL6KSAH920Whc-rd7ebuM/edit?usp=sharing} | ||
#' | ||
#' The Downballot, "Daily Kos Elections 2020 presidential election results for congressional districts used in 2024 elections" | ||
#' \url{https://docs.google.com/spreadsheets/d/1Sg4ZZz5FcX7lz-m2xqmYtndaO2uEMSaL7x99AbQOvv8/edit?usp=sharing} | ||
#' | ||
#' The Downballot, "Daily Kos Elections 2020 presidential results by congressional district (old CDs used in 2020 vs. new CDs used in 2022)" | ||
#' \url{https://docs.google.com/spreadsheets/d/1IfZ8OVWXVpdAvxZtTaDIA2HEN6DtN-H0I0J2KcdxRi4/edit?usp=sharing} | ||
#' | ||
#' The Downballot, "Daily Kos Elections congressional district geographic descriptions & largest places (119th Congress)" | ||
#' \url{https://docs.google.com/spreadsheets/d/12YaBonkqHAjkXhzyKlH2-1t-smZ6J5j76RCBSJEwQHo/edit?usp=sharing} | ||
#' | ||
#' The Downballot, "Daily Kos Elections congressional district geographic descriptions & largest places (118th Congress)" | ||
#' \url{https://docs.google.com/spreadsheets/d/1weoLFu2U5lmxQNcB8pFItGHj1Lb_M2E9Oi48sI4w1vY/edit?usp=sharing} | ||
#' | ||
#' The Downballot, "Congressional district geographic descriptions and largest places (117th Congress)" | ||
#' \url{https://docs.google.com/spreadsheets/d/16GoSYgRx5Mqfyfd355FMDRtr13tOAXFDOT94d9Dzwt0/edit?usp=sharing} | ||
#' | ||
#' Also see Cha, Jeremiah; Kuriwaki, Shiro; Snyder, James M. Jr., 2021, | ||
#' "Candidates in American General Elections", https://doi.org/10.7910/DVN/DGDRDT, | ||
#' Harvard Dataverse. | ||
#' | ||
#' @importFrom tibble tibble | ||
#' | ||
#' @examples | ||
#' head(cd_info_2022) | ||
"cd_info_2022" | ||
|
||
|
||
#' @rdname cd_info_2022 | ||
"cd_info_2024" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
library(tidyverse) | ||
library(googlesheets4) | ||
|
||
# Authenticate with Google Sheets | ||
gs4_auth() | ||
|
||
# URLs for your Google Sheets | ||
url_2022 <- "https://docs.google.com/spreadsheets/d/1CKngqOp8fzU22JOlypoxNsxL6KSAH920Whc-rd7ebuM/edit?usp=sharing" | ||
url_2024 <- "https://docs.google.com/spreadsheets/d/1Sg4ZZz5FcX7lz-m2xqmYtndaO2uEMSaL7x99AbQOvv8/edit?usp=sharing" | ||
url_geo_119 <- "https://docs.google.com/spreadsheets/d/12YaBonkqHAjkXhzyKlH2-1t-smZ6J5j76RCBSJEwQHo/edit?usp=sharing" | ||
url_geo_118 <- "https://docs.google.com/spreadsheets/d/1weoLFu2U5lmxQNcB8pFItGHj1Lb_M2E9Oi48sI4w1vY/edit?usp=sharing" | ||
|
||
# Read data from Google Sheets | ||
|
||
# 2022 | ||
cd_info_2022 <- read_sheet(url_2022, sheet = 1) %>% | ||
mutate(year = 2022) %>% | ||
select(year, cd = District) | ||
|
||
voting_info_2022 <- read_sheet(url_2022, sheet = 2) %>% | ||
select(cd = District, | ||
presvotes_total = Total, | ||
pct_trump = 'Trump %') | ||
|
||
region_2022 <- read_sheet(url_geo_118, sheet = 1) %>% | ||
select(cd = CD, | ||
dailykos_name = `Geographic Description`) | ||
|
||
largest_place_2022 <- read_sheet(url_geo_118, sheet = 2) %>% | ||
select(cd = CD, | ||
largest_place = `Largest place`) | ||
|
||
# 2024 | ||
cd_info_2024 <- read_sheet(url_2024, sheet = 1) %>% | ||
mutate(year = 2024) %>% | ||
select(year, cd = District) | ||
|
||
voting_info_2024 <- read_sheet(url_2024, sheet = 2) %>% | ||
select(cd = District, | ||
presvotes_total = Total, | ||
pct_trump = 'Trump %') | ||
|
||
region_2024 <- read_sheet(url_geo_119, sheet = 1) %>% | ||
select(cd = CD, | ||
dailykos_name = `Geographic Description`) | ||
|
||
largest_place_2024 <- read_sheet(url_geo_119, sheet = 2) %>% | ||
select(cd = CD, | ||
largest_place = `Largest place`) | ||
|
||
# Join geographic descriptions to the main datasets | ||
cd_info_2022 <- cd_info_2022 %>% | ||
left_join(region_2022, by = "cd") %>% | ||
left_join(largest_place_2022, by = "cd") %>% | ||
left_join(voting_info_2022, by = "cd") | ||
|
||
cd_info_2024 <- cd_info_2024 %>% | ||
left_join(region_2024, by = "cd") %>% | ||
left_join(largest_place_2024, by = "cd") %>% | ||
left_join(voting_info_2024, by = "cd") | ||
|
||
# Save the data | ||
usethis::use_data(cd_info_2022, overwrite = TRUE) | ||
usethis::use_data(cd_info_2024, overwrite = TRUE) |
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.