11library(tidyverse )
22library(googlesheets4 )
3+ library(janitor )
34
45# Authenticate with Google Sheets
56# gs4_auth()
67
78# URLs for your Google Sheets
89url_2022 <- " https://docs.google.com/spreadsheets/d/1CKngqOp8fzU22JOlypoxNsxL6KSAH920Whc-rd7ebuM/edit?usp=sharing"
9- url_2024 <- " https://docs.google.com/spreadsheets/d/1Sg4ZZz5FcX7lz-m2xqmYtndaO2uEMSaL7x99AbQOvv8/edit?usp=sharing"
10- url_geo_119 <- " https://docs.google.com/spreadsheets/d/12YaBonkqHAjkXhzyKlH2-1t-smZ6J5j76RCBSJEwQHo/edit?usp=sharing"
10+ url_2024 <- " https://docs.google.com/spreadsheets/d/1ng1i_Dm_RMDnEvauH44pgE6JCUsapcuu8F2pCfeLWFo/edit?gid=1491069057#gid=1491069057"
1111url_geo_118 <- " https://docs.google.com/spreadsheets/d/1weoLFu2U5lmxQNcB8pFItGHj1Lb_M2E9Oi48sI4w1vY/edit?usp=sharing"
12+ url_geo_119 <- " https://docs.google.com/spreadsheets/d/12YaBonkqHAjkXhzyKlH2-1t-smZ6J5j76RCBSJEwQHo/edit?usp=sharing"
1213
1314# Read data from Google Sheets
1415
@@ -24,30 +25,37 @@ voting_info_2022 <- read_sheet(url_2022, sheet = 2) |>
2425 presvotes_total = Total
2526 )
2627
27- region_2022 <- read_sheet(url_geo_118 , sheet = 1 ) | >
28+ region_2022 <- read_sheet(url_geo_118 , sheet = " Descriptive name " ) | >
2829 select(cd = CD ,
2930 dailykos_name = `Geographic Description` )
3031
31- largest_place_2022 <- read_sheet(url_geo_118 , sheet = 2 ) | >
32+ largest_place_2022 <- read_sheet(url_geo_118 , sheet = " By places " ) | >
3233 select(cd = CD ,
3334 largest_place = `Largest place` )
3435
3536# 2024
36- cd_names_2024 <- read_sheet(url_2024 , sheet = 1 ) | >
37- mutate(year = 2024 ) | >
38- select(year , cd = District )
37+ # clean sheet
38+ sheet_2024 <- read_sheet(url_2024 , sheet = " Vote totals" ) | >
39+ select(1 : 9 ) | >
40+ row_to_names(row_number = 2 , remove_row = TRUE ) | >
41+ select(cd = 1 , Harris , Trump , Total )
3942
40- voting_info_2024 <- read_sheet(url_2024 , sheet = 2 ) | >
41- transmute(cd = District ,
42- pct_trump20 = Trump / (Biden + Trump ),
43- presvotes_DR_20 = Biden + Trump ,
44- presvotes_total_20 = Total )
43+ cd_names_2024 <- sheet_2024 | >
44+ transmute(year = 2024 , cd )
45+
46+ voting_info_2024 <- sheet_2024 | >
47+ mutate(across(where(is.list ) & ! matches(" ^cd$" ),
48+ ~ map_dbl(.x , ~ as.numeric(.x [1 ])))) | >
49+ transmute(cd ,
50+ pct_trump = Trump / (Harris + Trump ),
51+ presvotes_DR = Harris + Trump ,
52+ presvotes_total = Total )
4553
46- region_2024 <- read_sheet(url_geo_119 , sheet = 1 ) | >
54+ region_2024 <- read_sheet(url_geo_119 , sheet = " Descriptive name " ) | >
4755 select(cd = CD ,
4856 dailykos_name = `Geographic Description` )
4957
50- largest_place_2024 <- read_sheet(url_geo_119 , sheet = 2 ) | >
58+ largest_place_2024 <- read_sheet(url_geo_119 , sheet = " By places " ) | >
5159 select(cd = CD ,
5260 largest_place = `Largest place` )
5361
0 commit comments