diff --git a/app/queries.R b/app/queries.R index 0c7bb8a..e42bca0 100644 --- a/app/queries.R +++ b/app/queries.R @@ -418,41 +418,9 @@ get_normalized_metric_growth <- function(storm, full_lf_id) { ) %>% select(state_fips, county_fips) - baseline_metrics <- get_tbl("pop_and_housing", "metrics") %>% - filter( - year == storm$storm_year - ) %>% + query <- get_tbl("pop_housing_normalized_growth", "metrics") %>% + filter(base_year == storm$storm_year) %>% inner_join(affected_counties, by = c("state_fips", "county_fips")) %>% - select( - state_fips, - county_fips, - baseline_population = population, - baseline_housing = housing_units - ) - - normalized_metrics <- get_tbl("pop_and_housing", "metrics") %>% - filter( - year >= storm$storm_year - ) %>% - inner_join(affected_counties, by = c("state_fips", "county_fips")) %>% - inner_join(baseline_metrics, by = c("state_fips", "county_fips")) %>% - mutate( - normalized_population = as.numeric(population) / - as.numeric(baseline_population), - normalized_housing = as.numeric(housing_units) / - as.numeric(baseline_housing) - ) %>% - select( - state_fips, - county_fips, - year, - population, - housing_units, - normalized_population, - normalized_housing - ) - - query <- normalized_metrics %>% inner_join( get_tbl("counties", "public"), by = c("state_fips" = "statefp", "county_fips" = "countyfp") @@ -469,11 +437,31 @@ get_normalized_metric_growth <- function(storm, full_lf_id) { name, population, housing_units, - normalized_population, - normalized_housing, + base_year_population, + base_year_housing, + normalized_population = population_normalized, + normalized_housing = housing_units_normalized, geom_wkt ) %>% arrange(state_fips, county_fips, year) + + result <- query %>% + collect() %>% + mutate( + normalized_population = if_else( + is.na(base_year_population) | is.na(base_year_housing), + NA_real_, + normalized_population / 100 + ), + normalized_housing = if_else( + is.na(base_year_population) | is.na(base_year_housing), + NA_real_, + normalized_housing / 100 + ) + ) %>% + select(-base_year_population, -base_year_housing) + + return(result) } else { affected_state <- get_tbl("indirect_landfalls", "gis") %>% filter(