Skip to content

Commit

Permalink
2022 and 2024 data
Browse files Browse the repository at this point in the history
Created a version of datadoc_cd-info and create_dist-level-data for 2022 and 2024 data
  • Loading branch information
MirandaSelin committed Sep 14, 2024
1 parent 6052f73 commit b26d4df
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 2 deletions.
2 changes: 1 addition & 1 deletion R/datadoc_cd-info.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#' 2018 given by Daily Kos. Some edits are made for changing district. See
#' Source for full citation.}
#' \item{largest_place}{The largest place in the district code in 2018 given by Daily Kos. Multiple districts may
#' have the largest place.}
#' have the same largest place.}
#' }
#'
#'
Expand Down
68 changes: 68 additions & 0 deletions R/new_datadoc_cd-info.R
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"
64 changes: 64 additions & 0 deletions data-raw/new_create_dist-level-data.R
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 added data/cd_info_2022.rda
Binary file not shown.
Binary file added data/cd_info_2024.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion man/cd_info_2018.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions man/cd_info_2022.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b26d4df

Please sign in to comment.