Skip to content

Commit

Permalink
2006, 2012 data
Browse files Browse the repository at this point in the history
-created create_dist-level-data_2022-2024.R to create cd_info_2006 and cd_info_2012
-fixed cd_info_2022 to match documentation
-added sources to datadoc_cd-info
  • Loading branch information
MirandaSelin committed Sep 30, 2024
1 parent af4e095 commit d249d2e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 27 deletions.
40 changes: 17 additions & 23 deletions R/datadoc_cd-info.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,17 @@
#' \item{pct_trump, pct_romney, pct_mccain}{The two-party voteshare of Republican
#' presidential candidates in that district for the given year. E.g. the
#' \code{pct_mccain} data for `cd_info_2018` represents the percent
#' of the vote by McCain in 2008 for that district _under 2018 lines._
#' `pct_trump` denotes the 2016 election for `cd_info_2018` and ``cd_info_2016`, but it is not
#' for 2020 where we use Trump's 2020 vote against Biden for `pct_trump` and denote as
#' `pct_trump16` the 2016 result.}
#' of the vote by McCain in 2008 for that district _under 2018 lines._\cr
#' `pct_trump` denotes the 2016 election for `cd_info_2018` and `cd_info_2016`.\cr
#' `pct_trump` denotes the 2020 election for `cd_info_2020`, `cd_info_2022`, and `cd_info_2024`.\cr
#' `pct_trump16` denotes the 2016 result for `cd_info_2020`.}
#' \item{dailykos_name}{The unique descriptive name for the district code in
#' 2018 given by Daily Kos (later renamed to 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 2018 given by Daily Kos. Multiple districts may
#' have the same largest place.}
#' }
#'
#' Congressional District Changes between 2022 and 2024:
#' Includes legal rationale for off-cycle redistricting.
#' \itemize{
#' \item{Alabama: state CD map altered such that AL-02 is a majority Black district.}
#' \item{Georgia: state CD map altered such that GA-06 is a majority Black district.}
#' \item{Louisiana: state CD map altered such that LA-06 is a majority Black district.}
#' \item{North Carolina: state Supreme Court's plan in 2022 expired.}
#' \item{New York: state Supreme Court stuck down original plan as partisan.}
#' }
#'
#' @source
#' The Downballot (formerly Daily Kos Elections), \url{https://www.the-downballot.com/p/data}
Expand All @@ -53,26 +44,24 @@
#' \url{https://bit.ly/2XsFI5W}
#'
#' Daily Kos, "2008, 2012, & 2016 results for districts used in 2018."
#' \url{https://bit.ly/3DRhPcj}

#' Daily Kos Elections 2012, 2016 & 2020 presidential election results for congressional districts in 2020"
#' \url{https://bit.ly/3bXtAPB}
#'
#' \url{https://bit.ly/3bXtAPB}
#'
#' Daily Kos, "2012, 2016 & 2020 presidential election results for congressional districts in 2020"
#' \url{https://bit.ly/3DRhPcj}
#'
#' Daily Kos, 2020 presidential election results by later congressional districts:\cr
#' 2022 congressional districts: \url{https://bit.ly/4gLYnBK}\cr
#' 2024 congressional districts: \url{https://bit.ly/47KTvZw}\cr
#' 2020, 2022 congressional district comparison: \url{https://bit.ly/3XNWOKI}
#' 2024 congressional districts: \url{https://bit.ly/47KTvZw}
#'
#' The Daily Koss congressional district geography and most populous places: \cr
#' Daily Kos, congressional district geography and most populous places: \cr
#' 119th Congress: \url{https://bit.ly/geography_119}\cr
#' 118th Congress: \url{https://bit.ly/geography_118}\cr
#' 117th Congress: \url{https://bit.ly/geography_117}
#' 118th Congress: \url{https://bit.ly/geography_118}
#'
#' Daily Kos, "2008 results for districts used in 2006, 2008, 2010"
#' \url{https://bit.ly/4entUrV}
#'
#' Daily Kos, "2008, 2012 results for districts used in 2012, 2014"
#' \url{https://bit.ly/3N4PDZK}
#'
#' Pennsylvania 2016 CD names are named by Shiro Kuriwaki and Lara Putnam.
#'
Expand All @@ -86,6 +75,11 @@
#' head(elec_NY)
"cd_info_2018"

#' @rdname cd_info_2018
"cd_info_2006"

#' @rdname cd_info_2018
"cd_info_2012"

#' @rdname cd_info_2018
"cd_info_2016"
Expand Down
44 changes: 44 additions & 0 deletions data-raw/create_dist-level-data_2006_2012.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
library(tidyverse)
library(googlesheets4)

# Authenticate with Google Sheets
gs4_auth()

# URLs for your Google Sheets
url_2006 <- "https://docs.google.com/spreadsheets/d/1l7W130tPRF6dQ4JoqlQJSJexnEg4rZct-7kwDXqgoLE/edit?gid=429358610#gid=429358610"
url_2012 <- "https://docs.google.com/spreadsheets/d/1xn6nCNM97oFDZ4M-HQgoUT3X4paOiSDsRMSuxbaOBdg/edit?gid=0#gid=0"

# Read data from Google Sheets

# 2006
cd_names_2006 <- read_sheet(url_2006, range = "A2:E", col_names = TRUE, sheet = 1) |>
mutate(year = 2006) |>
select(year, cd = CD)

voting_info_2006 <- read_sheet(url_2006,range = "A2:I", col_names = TRUE, sheet = 2) |>
select(cd = CD,
pct_mccain = 'McCain%')

# 2012
cd_names_2012 <- read_sheet(url_2012, range = "A2:G", col_names = TRUE, sheet = 1) |>
mutate(year = 2012) |>
select(year, cd = CD)

voting_info_2012 <- read_sheet(url_2012, range = "A2:O", col_names = TRUE, sheet = 2) |>
select(cd = CD,
presvotes_total = 7,
pct_romney = 'Romney%',
pct_mccain = 'McCain%')

# Join data from page 1 and page 2 for each dataset
cd_info_2006 <- cd_names_2006 |>
left_join(voting_info_2006, by = "cd") |>
mutate(cd = str_replace(cd, "-AL$", "-01"))

cd_info_2012 <- cd_names_2012 |>
left_join(voting_info_2012, by = "cd") |>
mutate(cd = str_replace(cd, "-AL$", "-01"))

# Save the data
usethis::use_data(cd_info_2022, overwrite = TRUE)
usethis::use_data(cd_info_2024, overwrite = TRUE)
1 change: 0 additions & 1 deletion data-raw/create_dist-level-data_2022-2024.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ cd_names_2022 <- read_sheet(url_2022, sheet = 1) |>

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) |>
Expand Down
Binary file modified data/cd_info_2022.rda
Binary file not shown.
Binary file modified data/cd_info_2024.rda
Binary file not shown.
7 changes: 4 additions & 3 deletions man/cd_info_2018.Rd

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

0 comments on commit d249d2e

Please sign in to comment.