Skip to content

Commit

Permalink
Change "map" variable to "lines" -- maybe less confusing
Browse files Browse the repository at this point in the history
  • Loading branch information
kuriwaki committed Oct 7, 2024
1 parent 224dce2 commit 5cc29d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
12 changes: 7 additions & 5 deletions R/datadoc_cd-info-long.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' covering the maps of `r length(unique(cd_info_long$map))` election years
#' for each of the 435 congressional districts.
#' \describe{
#' \item{`map`}{Is the year corresponding to the geography. For example, `map = 2008` and `cd = "AL-01`
#' \item{`lines`}{Is the year corresponding to the geography. For example, `map = 2008` and `cd = "AL-01`
#' indicates that the row is representing AL-01's geography as used in the 2008 election.}
#' \item{`cd`}{Is the CD corresponding to the year of the map.}
#' \item{`elec`}{Is the year of the election for the presidential election data that follows}
Expand All @@ -20,16 +20,18 @@
#' }
#'
#' @examples
#' library(dplyr)
#'
#' # get only data for proximate years
#' cd_info_long |> filter((map == elec) | (elec + 2 == map))
#' cd_info_long |> filter((map == lines) | (elec + 2 == lines))
#'
#' # this subset returns exactly 2 * 435 districts per cycle:
#' cd_info_long |> filter((map == elec) | (elec + 2 == map)) |> count(map, party)
#' cd_info_long |> filter((map == lines) | (elec + 2 == lines)) |> count(lines, party)
#'
#' # this will show where the districts lines changed between 2022 and 2024
#' # (same election, same candidate, different map)
#' cd_info_long |>
#' filter(map %in% c(2022, 2024), elec == 2020, candidate == "biden") |>
#' arrange(cd, map)
#' filter(lines %in% c(2022, 2024), elec == 2020, candidate == "biden") |>
#' arrange(cd, lines)
#'
"cd_info_long"
24 changes: 12 additions & 12 deletions data-raw/create_cd-info-long.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ cd_info_all <- bind_rows(
# Republican vote percentages
R_pct <-
cd_info_all |>
select(map = year, cd, starts_with("pct_")) |>
select(lines = year, cd, starts_with("pct_")) |>
pivot_longer(
matches("pct_"),
names_prefix = "pct_",
Expand All @@ -58,8 +58,8 @@ R_pct <-
name == "romney" ~ 2012,
name == "trump16" ~ 2016,
name == "trump20" ~ 2020,
name == "trump" & map <= 2018 ~ 2016,
name == "trump" & map >= 2020 & map <= 2022 ~ 2020
name == "trump" & lines <= 2018 ~ 2016,
name == "trump" & lines >= 2020 & lines <= 2022 ~ 2020
),
.after = cd
) |>
Expand All @@ -69,7 +69,7 @@ R_pct <-

D_pct <- R_pct |>
left_join(pres_names_wide, by = "elec") |>
transmute(map,
transmute(lines,
cd,
elec,
party = "D",
Expand All @@ -79,26 +79,26 @@ D_pct <- R_pct |>
# same for Ns
Ns <-
cd_info_all |>
select(map = year, cd, matches("total")) |>
select(lines = year, cd, matches("total")) |>
pivot_longer(
matches("presvotes_total"),
names_prefix = "presvotes_",
values_to = "presvotes_total", values_drop_na = TRUE) |>
mutate(elec = case_when(
name == "total20" ~ 2020,
map %in% c(2008, 2012, 2016, 2020) ~ map,
map == 2010 ~ 2008,
map == 2014 ~ 2012,
map == 2018 ~ 2016,
map == 2022 ~ 2020,
lines %in% c(2008, 2012, 2016, 2020) ~ lines,
lines == 2010 ~ 2008,
lines == 2014 ~ 2012,
lines == 2018 ~ 2016,
lines == 2022 ~ 2020,
),
.after = cd
) |>
select(-name)

cd_info_long <- bind_rows(D_pct, R_pct) |>
tidylog::left_join(Ns, by = c("map", "cd", "elec")) |>
arrange(map, elec, cd, party) |>
tidylog::left_join(Ns, by = c("lines", "cd", "elec")) |>
arrange(lines, elec, cd, party) |>
rename(candidate = name)

usethis::use_data(cd_info_long, overwrite = TRUE)
Binary file modified data/cd_info_long.rda
Binary file not shown.

0 comments on commit 5cc29d4

Please sign in to comment.