From 2d42c4ff41f489f7ba6ebe66f0e8b2fead75686c Mon Sep 17 00:00:00 2001 From: Nat DeFries <42820733+nmdefries@users.noreply.github.com> Date: Thu, 18 Jan 2024 16:21:54 -0500 Subject: [PATCH] wip support different types of time_step/time_type --- R/slide.R | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/R/slide.R b/R/slide.R index f770d6e8a..a39829391 100644 --- a/R/slide.R +++ b/R/slide.R @@ -567,16 +567,32 @@ epi_slide_mean = function(x, col_name, ..., before, after, ref_time_values, # after <- time_step(after) # } - # time_step can be any of `c("days", "weeks", "months", "quarters", "years")` - all_dates <- seq(min(x$time_value), max(x$time_value), by = time_step) + all_dates <- as.Date(c()) + pad_early_dates <- as.Date(c()) + pad_late_dates <- as.Date(c()) - pad_early_dates <- c() - pad_late_dates <- c() - if (before != 0) { - pad_early_dates <- all_dates[1L] - before:1 - } - if (after != 0) { - pad_late_dates <- all_dates[length(all_dates)] + 1:after + # Find `by` corresponding to epi_df `time_type` or `time_step`. + by <- ... + + if (!is.null(by)) { + # time_step can be any of `c("days", "weeks", "months", "quarters", "years")` + # tidyr::full_seq(c(min(x$time_value), max(x$time_value)), period = 1) ? + all_dates <- seq(min(x$time_value), max(x$time_value), by = by) + + if (before != 0) { + pad_early_dates <- Start(all_dates) - before:1 + } + if (after != 0) { + pad_late_dates <- End(all_dates) + 1:after + } + } else { + Warn( + c( + "`frollmean` requires a full window to compute a result. Add NA values + to beginning and end of the group so that we get results for the + first `before` and last `after` elements." + ) + ) } # `frollmean` is 1-indexed, so create a new window width based on our