Code
library(rvest)
library(tidyverse)
library(here)
library(glue)
library(rvest)
library(tidyverse)
library(here)
library(glue)
The urls to the killed/euthized tables are the following:
= list(
killed_euthanized "2023" = "https://www.aphis.usda.gov/pdr/PDR-G_Report?p=2023:KILLED_EUTH:0:k6Slc6m5armtqanTaYXJqp-Rrg==",
"2022" = "https://www.aphis.usda.gov/pdr/PDR-G_Report?p=2022:KILLED_EUTH:0:k6Slcqm5armtqanTaYXJqp-Rrg==",
"2021" = "https://www.aphis.usda.gov/pdr/PDR-G_Report?p=2021:KILLED_EUTH:0:k6Slcam5armtqanTaYXJqp-Rrg==",
"2020" = "https://www.aphis.usda.gov/pdr/PDR-G_Report?p=2020:KILLED_EUTH:0:k6SlcKm5armtqanTaYXJqp-Rrg==",
"2019" = "https://www.aphis.usda.gov/pdr/PDR-G_Report?p=2019:KILLED_EUTH:0:k6Skeam5armtqanTaYXJqp-Rrg==",
"2018" = "https://www.aphis.usda.gov/pdr/PDR-G_Report?p=2018:KILLED_EUTH:0:k6SkeKm5armtqanTaYXJqp-Rrg==",
"2017" = "https://www.aphis.usda.gov/pdr/PDR-G_Report?p=2017:KILLED_EUTH:0:k6Skd6m5armtqanTaYXJqp-Rrg==",
"2016" = "https://www.aphis.usda.gov/pdr/PDR-G_Report?p=2016:KILLED_EUTH:0:k6Skdqm5armtqanTaYXJqp-Rrg==",
"2015" = "https://www.aphis.usda.gov/pdr/PDR-G_Report?p=2015:KILLED_EUTH:0:k6Skdam5armtqanTaYXJqp-Rrg==",
"2014" = "https://www.aphis.usda.gov/pdr/PDR-G_Report?p=2014:KILLED_EUTH:0:k6SkdKm5armtqanTaYXJqp-Rrg=="
)
killed_euthanized
Download all of the tables and combine into one dataframe
= function(item, type){
download_killed_report
= item[1][[1]]
url
<- read_html(url)
page
<- page %>%
table html_nodes("table") %>%
5] %>%
.[html_table(fill = TRUE)
if (type == "overall"){
<- as.data.frame(table) %>%
data_frame ::row_to_names(row_number = 1) %>%
janitor::clean_names() %>%
janitormutate(across(where(is.character), tolower)) %>%
filter(!str_detect(species, "species total"))
else if ( type == "state"){
}
<- as.data.frame(table) %>%
data_frame ::row_to_names(row_number = 1) %>%
janitor::clean_names() %>%
janitormutate(across(where(is.character), tolower)) %>%
filter(capture_restraint_dispersal_method != "totals")
}
return(data_frame)
}
= killed_euthanized %>%
all_killed_euthanized ::map(\(x){download_killed_report(x, "overall")}, .progress = TRUE) %>%
purrr::list_rbind(names_to = "year") purrr
glimpse(all_killed_euthanized)
write_csv(all_killed_euthanized, here("data/processed/20240926--all_killed_euthanized_2014-2023.csv"))
This is the format of a url: https://www.aphis.usda.gov/pdr/PDR-G_Report?p=2015:STATE:AK:k6SkdanBda61qZ-1b2o=
The code below creates all the urls for each state for each year
# Found by using inspect elements on webpage
# ==========================================
= list("2023" = "k6Slc6nBda61qZ",
year_codes "2022" = "k6SlcqnBda61qZ",
"2021" = "k6SlcanBda61qZ",
"2020" = "k6SlcKnBda61qZ",
"2019" = "k6SkeanBda61qZ",
"2018" = "k6SkeKnBda61qZ",
"2017" = "k6Skd6nBda61qZ",
"2016" = "k6SkdqnBda61qZ",
"2015" = "k6SkdanBda61qZ",
"2014" = "k6SkdKnBda61qZ")
= list(
state_codes 'AK' = '-1b2o=',
'AL' = '-1cGo=',
'AR' = '-1dmo=',
'AZ' = '-1fmo=',
'CA' = '-3ZWo=',
'CO' = '-3c2o=',
'CT' = '-3eGo=',
'DC' = '-4Z2o=',
'DD' = '-4aGo=',
'DE' = '-4aWo=',
'FL' = '-6cGo=',
'GA' = '-7ZWo=',
'GU' = '-7eWo=',
'HI' = '-8bWo=',
'IA' = '-9ZWo=',
'ID' = '-9aGo=',
'IL' = '-9cGo=',
'IN' = '-9cmo=',
'KS' = '-_d2o=',
'KY' = '-_fWo=',
'LA' = '_AZWo=',
'MA' = '_BZWo=',
'MD' = '_BaGo=',
'ME' = '_BaWo=',
'MI' = '_BbWo=',
'MN' = '_Bcmo=',
'MO' = '_Bc2o=',
'MS' = '_Bd2o=',
'MT' = '_BeGo=',
'NC' = '_CZ2o=',
'ND' = '_CaGo=',
'NE' = '_CaWo=',
'NH' = '_CbGo=',
'NJ' = '_Cbmo=',
'NM' = '_CcWo=',
'NV' = '_Cemo=',
'NY' = '_CfWo=',
'OH' = '_DbGo=',
'OK' = '_Db2o=',
'OR' = '_Ddmo=',
'PA' = '_EZWo=',
'PR' = '_Edmo=',
'RI' = '_GbWo=',
'SC' = '_HZ2o=',
'SD' = '_HaGo=',
'TN' = '_Icmo=',
'TX' = '_IfGo=',
'UT' = '_JeGo=',
'VA' = '_KZWo=',
#'None' = '_Jd4a-nA==',
'VT' = '_KeGo=',
'WA' = '_LZWo=',
'WI' = '_LbWo=',
'WV' = '_Lemo=',
'WY' = '_LfWo=')
# Create urls
# ============
<- expand.grid(year = names(year_codes), state = names(state_codes))
year_state_combos
= function(year, state){
create_url = year_codes[[year]]
year_code = state_codes[[state]]
state_code = glue("https://www.aphis.usda.gov/pdr/PDR-G_Report?p={year}:STATE:{state}:{year_code}{state_code}")
url return(url)
}
<- setNames(
state_urls map2(year_state_combos$year, year_state_combos$state, ~ create_url(.x, .y)),
$year
year_state_combos )
# Download state tables
= state_urls %>%
all_states_df ::map(\(x){download_killed_report(x, "state")}, .progress = TRUE) %>%
purrr::list_rbind(names_to = "year") purrr
glimpse(all_states_df)
write_csv(all_states_df, here("data/processed/20240926--all_states_2014-2023.csv"))
https://www.aphis.usda.gov/pdr/PDR-C_Report?p=2023:STATE:TX:k6Slc6nBda61qZ_IfGo=
https://www.aphis.usda.gov/pdr/PDR-C_Report?p=2023:INDEX: