Skip to content

Commit ad15b8f

Browse files
committed
Add unit_time_delta to make time_delta_to_n_steps make sense
1 parent 632c8d9 commit ad15b8f

File tree

5 files changed

+100
-2
lines changed

5 files changed

+100
-2
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export(slice)
9898
export(sum_groups_epi_df)
9999
export(time_column_names)
100100
export(ungroup)
101+
export(unit_time_delta)
101102
export(unnest)
102103
export(validate_epi_archive)
103104
export(version_column_names)

R/utils.R

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ validate_slide_window_arg <- function(arg, time_type, lower = 1, allow_inf = TRU
11221122
#' @param time_type as in `validate_slide_window_arg`
11231123
#' @return [bare integerish][rlang::is_integerish] vector (with possible
11241124
#' infinite values) that produces the same result as `time_delta` when
1125-
#' multiplied by the "natural" "unit time delta" (not yet implemented) for
1125+
#' multiplied by the natural [`unit_time_delta`] for
11261126
#' that time type and added to time values of time type `time_type`. If the
11271127
#' given time type does not support infinite values, then it should produce
11281128
#' +Inf or -Inf for analogous entries of `time_delta`, and match the addition
@@ -1157,6 +1157,26 @@ time_delta_to_n_steps <- function(time_delta, time_type) {
11571157
}
11581158
}
11591159

1160+
#' Object that, added to time_values of time_type, advances by one time step/interval
1161+
#'
1162+
#' @param time_type string; `epi_df`'s or `epi_archive`'s `time_type`
1163+
#' @return an object `u` such that `time_values + u` represents advancing by one
1164+
#' time step / moving to the subsequent time interval for any `time_values`
1165+
#' object of time type `time_type`, and such that `time_values + k * u` for
1166+
#' integerish vector `k` advances by `k` steps (with vectorization,
1167+
#' recycling).
1168+
#'
1169+
#' @export
1170+
unit_time_delta <- function(time_type) {
1171+
switch(time_type,
1172+
day = as.difftime(1, units = "days"),
1173+
week = as.difftime(1, units = "weeks"),
1174+
yearmonth = 1,
1175+
integer = 1L,
1176+
cli_abort("Unsupported time_type: {time_type}")
1177+
)
1178+
}
1179+
11601180
# Using these unit abbreviations happens to make our automatic slide output
11611181
# naming look like taking ISO-8601 duration designations, removing the P, and
11621182
# lowercasing any characters. Fortnightly or sub-daily time types would need an

man/time_delta_to_n_steps.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/unit_time_delta.Rd

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-utils.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,59 @@ test_that("validate_slide_window_arg works", {
362362
class = "epiprocess__validate_slide_window_arg"
363363
)
364364
})
365+
366+
test_that("unit_time_delta works", {
367+
expect_equal(
368+
as.Date("2020-01-01") + 5 * unit_time_delta("day"),
369+
as.Date("2020-01-06")
370+
)
371+
expect_equal(
372+
as.Date("2020-01-01") + 2 * unit_time_delta("week"),
373+
as.Date("2020-01-15")
374+
)
375+
expect_equal(
376+
tsibble::make_yearmonth(2000, 1) + 5 * unit_time_delta("yearmonth"),
377+
tsibble::make_yearmonth(2000, 6)
378+
)
379+
expect_equal(
380+
1L + 5L * unit_time_delta("integer"),
381+
6L
382+
)
383+
#
384+
expect_equal(
385+
as.Date("2020-01-01") +
386+
time_delta_to_n_steps(as.Date("2020-01-06") - as.Date("2020-01-01"), "day") *
387+
unit_time_delta("day"),
388+
as.Date("2020-01-06")
389+
)
390+
expect_equal(
391+
as.Date("2020-01-01") +
392+
time_delta_to_n_steps(as.integer(as.Date("2020-01-06") - as.Date("2020-01-01")), "day") *
393+
unit_time_delta("day"),
394+
as.Date("2020-01-06")
395+
)
396+
expect_equal(
397+
as.Date("2020-01-01") +
398+
time_delta_to_n_steps(as.Date("2020-01-15") - as.Date("2020-01-01"), "week") *
399+
unit_time_delta("week"),
400+
as.Date("2020-01-15")
401+
)
402+
expect_equal(
403+
as.Date("2020-01-01") +
404+
time_delta_to_n_steps(as.difftime(2, units = "weeks"), "week") *
405+
unit_time_delta("week"),
406+
as.Date("2020-01-15")
407+
)
408+
expect_equal(
409+
tsibble::make_yearmonth(2000, 1) +
410+
time_delta_to_n_steps(5, "yearmonth") *
411+
unit_time_delta("yearmonth"),
412+
tsibble::make_yearmonth(2000, 6)
413+
)
414+
expect_equal(
415+
1L +
416+
time_delta_to_n_steps(5, "integer") *
417+
unit_time_delta("integer"),
418+
6L
419+
)
420+
})

0 commit comments

Comments
 (0)