|
| 1 | +if (tryCatch(read.dcf("DESCRIPTION")[1, "Package"] == "codec", finally = FALSE)) { |
| 2 | + devtools::load_all() |
| 3 | +} else { |
| 4 | + library(codec) |
| 5 | +} |
| 6 | + |
| 7 | +library(dplyr, warn.conflicts = FALSE) |
| 8 | +library(readr, warn.conflicts = FALSE) |
| 9 | +library(addr) |
| 10 | +library(dpkg) |
| 11 | + |
| 12 | +sessionInfo() |
| 13 | + |
| 14 | +rd <- read_csv( |
| 15 | + glue::glue( |
| 16 | + "https://votehamiltoncountyohio.gov/download.php?file=VoterListExport-", |
| 17 | + format(Sys.Date(), "%Y%m%d"), |
| 18 | + "-no.csv" |
| 19 | + ), |
| 20 | + col_types = |
| 21 | + cols_only( |
| 22 | + AddressPreDirectional = col_character(), |
| 23 | + AddressNumber = col_double(), |
| 24 | + AddressStreet = col_character(), |
| 25 | + AddressSuffix = col_character(), |
| 26 | + CityName = col_character(), |
| 27 | + AddressZip = col_character(), |
| 28 | + `2024 General Election` = col_factor(), |
| 29 | + `2024 Primary Election` = col_factor(), |
| 30 | + `2023 General Election` = col_factor(), |
| 31 | + `2023 August Election` = col_factor(), |
| 32 | + PRIMARY_MAY_2023 = col_factor(), |
| 33 | + GENERAL_NOV_2022 = col_factor(), |
| 34 | + `AUG PRIMARY ELECTION 2022` = col_factor(), |
| 35 | + PRIMARY_MAY_2022 = col_factor(), |
| 36 | + GENERAL_NOV_2021 = col_factor(), |
| 37 | + PRIMARY_MAY_2021 = col_factor(), |
| 38 | + GENERAL_NOV_2020 = col_factor(), |
| 39 | + SPECIAL_AUG_2020 = col_factor() |
| 40 | + ) |
| 41 | +) |
| 42 | + |
| 43 | +d <- |
| 44 | + rd |> |
| 45 | + mutate(foofy_state = "OH") |> |
| 46 | + tidyr::unite("voter_address", |
| 47 | + c( |
| 48 | + AddressPreDirectional, AddressNumber, AddressStreet, |
| 49 | + AddressSuffix, CityName, foofy_state, AddressZip |
| 50 | + ), |
| 51 | + sep = " ", |
| 52 | + remove = TRUE, |
| 53 | + na.rm = TRUE |
| 54 | + ) |
| 55 | + |
| 56 | +cagis_s2 <- |
| 57 | + cagis_addr()$cagis_addr_data |> |
| 58 | + purrr::modify_if(\(.) length(.) > 0 && nrow(.) > 1, dplyr::slice_sample, n = 1) |> |
| 59 | + purrr::map_vec(purrr::pluck, "cagis_s2", .default = NA, .ptype = s2::s2_cell()) |
| 60 | + |
| 61 | +d_geocode <- addr::addr_match_geocode( |
| 62 | + x = d$voter_address, |
| 63 | + ref_addr = cagis_addr()$cagis_addr, |
| 64 | + ref_s2 = cagis_s2, |
| 65 | + county = "39061", |
| 66 | + year = "2022" |
| 67 | +) |
| 68 | + |
| 69 | +d_geocode <- d_geocode |> |
| 70 | + mutate( |
| 71 | + bg = s2_join_tiger_bg(s2, "2023"), |
| 72 | + tract = substr(bg, 1, 11) |
| 73 | + ) |
| 74 | + |
| 75 | +d_rates <- |
| 76 | + bind_cols(d, d_geocode) |> |
| 77 | + group_by(tract) |> |
| 78 | + summarize(across( |
| 79 | + c( |
| 80 | + `2024 General Election`, `2024 Primary Election`, |
| 81 | + `2023 General Election`, `2023 August Election`, |
| 82 | + PRIMARY_MAY_2023, GENERAL_NOV_2022, |
| 83 | + `AUG PRIMARY ELECTION 2022`, PRIMARY_MAY_2022, |
| 84 | + GENERAL_NOV_2021, PRIMARY_MAY_2021, |
| 85 | + GENERAL_NOV_2020, SPECIAL_AUG_2020 |
| 86 | + ), |
| 87 | + \(.) sum(!is.na(.)) / length(.) |
| 88 | + )) |
| 89 | + |
| 90 | +out <- |
| 91 | + cincy::tract_tigris_2020 |> |
| 92 | + sf::st_drop_geometry() |> |
| 93 | + as_tibble() |> |
| 94 | + left_join(d_rates, by = c("census_tract_id_2020" = "tract")) |
| 95 | + |
| 96 | +out_dpkg <- |
| 97 | + out |> |
| 98 | + mutate(year = "2024") |> |
| 99 | + as_codec_dpkg( |
| 100 | + name = "voter_participation", |
| 101 | + version = glue::glue("0.1-", format(Sys.Date(), "%Y%m%d")), |
| 102 | + title = "Voter Participation Rates", |
| 103 | + homepage = "https://geomarker.io/codec", |
| 104 | + description = paste(readLines(fs::path_package("codec", "codec_data", "voter_participation", "README.md")), collapse = "\n") |
| 105 | + ) |
| 106 | + |
| 107 | +dpkg::dpkg_gh_release(out_dpkg, draft = FALSE) |
0 commit comments