feat(per-capita): use per-year F-33 population in spending verbs
cog_spending(per_capita = TRUE) and cog_revenue(per_capita = TRUE) now divide each year's amount by that gov-year's population from gov_population_yearly (drawn from long.population) instead of a single static ACS 2018-2022 value. Adds pop_source column with values 'census_f33' or 'unavailable'.
This commit is contained in:
+13
-7
@@ -143,18 +143,24 @@ cog_spending <- function(govid, years, category = NULL,
|
|||||||
.attach_per_capita <- function(result, con, govid) {
|
.attach_per_capita <- function(result, con, govid) {
|
||||||
if (nrow(result) == 0L) {
|
if (nrow(result) == 0L) {
|
||||||
result$amt_per_capita_nominal <- numeric(0)
|
result$amt_per_capita_nominal <- numeric(0)
|
||||||
|
result$pop_source <- character(0)
|
||||||
return(result)
|
return(result)
|
||||||
}
|
}
|
||||||
|
years_lit <- paste(unique(as.integer(result$year)), collapse = ",")
|
||||||
sql <- sprintf(
|
sql <- sprintf(
|
||||||
"SELECT canonical_govid, population_acs
|
"SELECT canonical_govid, year, population
|
||||||
FROM canonical_fips_xwalk
|
FROM gov_population_yearly
|
||||||
WHERE canonical_govid IN (%s)",
|
WHERE canonical_govid IN (%s)
|
||||||
.sql_lit_chr(govid)
|
AND year IN (%s)",
|
||||||
|
.sql_lit_chr(govid), years_lit
|
||||||
)
|
)
|
||||||
pops <- tibble::as_tibble(DBI::dbGetQuery(con, sql))
|
pops <- tibble::as_tibble(DBI::dbGetQuery(con, sql))
|
||||||
result <- dplyr::left_join(result, pops, by = "canonical_govid")
|
result <- dplyr::left_join(result, pops,
|
||||||
result$amt_per_capita_nominal <- result$amt_nominal / result$population_acs
|
by = c("canonical_govid", "year"))
|
||||||
result$population_acs <- NULL
|
result$amt_per_capita_nominal <- result$amt_nominal / result$population
|
||||||
|
result$pop_source <- ifelse(is.na(result$population),
|
||||||
|
"unavailable", "census_f33")
|
||||||
|
result$population <- NULL
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user