From 5e990ec10dc1715c50b6ec8792e105bc92ef3ad7 Mon Sep 17 00:00:00 2001 From: Damonamajor Date: Wed, 7 Aug 2024 17:28:20 +0000 Subject: [PATCH] Add spatial joins to if_else --- analyses/Data_transformation.R | 35 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/analyses/Data_transformation.R b/analyses/Data_transformation.R index 073933c2..d9836510 100644 --- a/analyses/Data_transformation.R +++ b/analyses/Data_transformation.R @@ -88,26 +88,25 @@ if (type == "continuous") { median(!!sym(target_feature_value), na.rm = TRUE), !!paste0(target_feature_value, "_neighborhood_90th") := quantile(!!sym(target_feature_value), 0.9, na.rm = TRUE) - ) -} else { - { - pin_nbhd <- pin_individual %>% - group_by(meta_nbhd_code, !!sym({{ target_feature_value }})) %>% - count() %>% - ungroup() %>% - group_by(meta_nbhd_code) %>% - mutate(percentage = n / sum(n) * 100) %>% - select(meta_nbhd_code, !!sym({{ target_feature_value }}), percentage) %>% - arrange(meta_nbhd_code, desc(percentage)) %>% # Arrange to have the highest percentage first - mutate(rank = row_number()) %>% - pivot_wider( - names_from = rank, - values_from = c(!!sym({{ target_feature_value }}), percentage), - names_glue = "{.value}_{rank}" - ) - } %>% + ) %>% inner_join(nbhd, by = c("meta_nbhd_code" = "town_nbhd")) %>% st_as_sf() +} else {{ pin_nbhd <- pin_individual %>% + group_by(meta_nbhd_code, !!sym({{ target_feature_value }})) %>% + count() %>% + ungroup() %>% + group_by(meta_nbhd_code) %>% + mutate(percentage = n / sum(n) * 100) %>% + select(meta_nbhd_code, !!sym({{ target_feature_value }}), percentage) %>% + arrange(meta_nbhd_code, desc(percentage)) %>% # Arrange to have the highest percentage first + mutate(rank = row_number()) %>% + pivot_wider( + names_from = rank, + values_from = c(!!sym({{ target_feature_value }}), percentage), + names_glue = "{.value}_{rank}" + ) %>% + inner_join(nbhd, by = c("meta_nbhd_code" = "town_nbhd")) %>% + st_as_sf() } } # Pivot wider for leaflet maps to allow multiple shap values