Skip to content

Commit 2838296

Browse files
committed
fix: data_substitutions
1 parent 31e9222 commit 2838296

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

R/utils.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,27 @@ get_exclusions <- function(
155155
}
156156

157157
data_substitutions <- function(dataset, disease, forecast_generation_date) {
158-
disease <- "flu"
158+
# Get the substitutions from the table, matched by forecast generation date
159159
substitutions <- readr::read_csv(
160160
glue::glue("{disease}_data_substitutions.csv"),
161161
comment = "#",
162162
show_col_types = FALSE
163163
) %>%
164164
filter(forecast_date == forecast_generation_date) %>%
165165
select(-forecast_date) %>%
166-
rename(new_value = value)
167-
dataset %>%
168-
left_join(substitutions) %>%
166+
rename(new_value = value) %>%
167+
select(-time_value)
168+
# Replace the most recent values in the appropriate keys with the substitutions
169+
new_values <- dataset %>%
170+
group_by(geo_value) %>%
171+
slice_max(time_value) %>%
172+
inner_join(substitutions) %>%
169173
mutate(value = ifelse(!is.na(new_value), new_value, value)) %>%
170174
select(-new_value)
175+
# Remove keys from dataset that have been substituted
176+
dataset %>%
177+
anti_join(new_values, by = c("geo_value", "time_value")) %>%
178+
bind_rows(new_values)
171179
}
172180

173181
parse_prod_weights <- function(filename = here::here("covid_geo_exclusions.csv"),

0 commit comments

Comments
 (0)