Skip to content

Commit 0e95b95

Browse files
committed
refactor+docs: move complete.epi_df tests to examples
* complete.epi_df now has its own docs page
1 parent b5d96e7 commit 0e95b95

File tree

4 files changed

+211
-51
lines changed

4 files changed

+211
-51
lines changed

R/methods-epi_df.R

+58-2
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,68 @@ group_modify.epi_df <- function(.data, .f, ..., .keep = FALSE) {
241241
dplyr::dplyr_reconstruct(NextMethod(), .data)
242242
}
243243

244-
#' @importFrom tidyr complete
245-
#' @rdname print.epi_df
244+
#' Complete epi_df
245+
#'
246+
#' A [tidyr::complete()] analogue for `epi_df` objects. This function fills in
247+
#' missing combinations of `geo_value` and `time_value` with `NA` values. See
248+
#' the examples for usage details.
249+
#'
246250
#' @param data an `epi_df`
251+
#' @param ... see [`tidyr::complete`]
247252
#' @param fill see [`tidyr::complete`]
248253
#' @param explicit see [`tidyr::complete`]
254+
#'
249255
#' @method complete epi_df
256+
#' @importFrom tidyr complete
257+
#'
258+
#' @examples
259+
#' start_date <- as.Date("2020-01-01")
260+
#' daily_edf <- tibble::tribble(
261+
#' ~geo_value, ~time_value, ~value,
262+
#' 1, start_date + 1, 1,
263+
#' 1, start_date + 3, 3,
264+
#' 2, start_date + 2, 2,
265+
#' 2, start_date + 3, 3,
266+
#' ) %>%
267+
#' as_epi_df(as_of = start_date + 3)
268+
#' # Complete without grouping puts all the geo_values on the same min and max
269+
#' # time_value index
270+
#' daily_edf %>%
271+
#' complete(geo_value, time_value = full_seq(time_value, period = 1))
272+
#' # Complete with grouping puts all the geo_values on individual min and max
273+
#' # time_value indices
274+
#' daily_edf %>%
275+
#' group_by(geo_value) %>%
276+
#' complete(time_value = full_seq(time_value, period = 1))
277+
#' # Complete has explicit=TRUE by default, but if it's FALSE, then complete only fills the implicit gaps
278+
#' # not those that are explicitly NA
279+
#' daily_edf <- tibble::tribble(
280+
#' ~geo_value, ~time_value, ~value,
281+
#' 1, start_date + 1, 1,
282+
#' 1, start_date + 2, NA,
283+
#' 1, start_date + 3, 3,
284+
#' 2, start_date + 2, 2,
285+
#' 2, start_date + 3, 3,
286+
#' ) %>%
287+
#' as_epi_df(as_of = start_date + 3)
288+
#' daily_edf %>%
289+
#' complete(geo_value, time_value = full_seq(time_value, period = 1), fill = list(value = 0), explicit = FALSE)
290+
#' # Complete works for weekly data and can take a fill value
291+
#' # No grouping
292+
#' weekly_edf <- tibble::tribble(
293+
#' ~geo_value, ~time_value, ~value,
294+
#' 1, start_date + 1, 1,
295+
#' 1, start_date + 15, 3,
296+
#' 2, start_date + 8, 2,
297+
#' 2, start_date + 15, 3,
298+
#' ) %>%
299+
#' as_epi_df(as_of = start_date + 3)
300+
#' weekly_edf %>%
301+
#' complete(geo_value, time_value = full_seq(time_value, period = 7), fill = list(value = 0))
302+
#' # With grouping
303+
#' weekly_edf %>%
304+
#' group_by(geo_value) %>%
305+
#' complete(time_value = full_seq(time_value, period = 7), fill = list(value = 0))
250306
#' @export
251307
complete.epi_df <- function(data, ..., fill = list(), explicit = TRUE) {
252308
result <- dplyr::dplyr_reconstruct(NextMethod(), data)

man/complete.epi_df.Rd

+71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/print.epi_df.Rd

-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-methods-epi_df.R

+82-42
Original file line numberDiff line numberDiff line change
@@ -180,76 +180,116 @@ test_that("Renaming columns while grouped gives appropriate colnames and metadat
180180

181181
test_that("complete.epi_df works", {
182182
start_date <- as.Date("2020-01-01")
183-
daily_edf <- tibble::tibble(
184-
geo_value = c(1, 2),
185-
time_value = start_date + c(1, 2),
186-
value = 1
183+
daily_edf <- tibble::tribble(
184+
~geo_value, ~time_value, ~value,
185+
1, start_date + 1, 1,
186+
1, start_date + 3, 3,
187+
2, start_date + 2, 2,
188+
2, start_date + 3, 3,
187189
) %>%
188190
as_epi_df(as_of = start_date + 3)
191+
# Complete without grouping puts all the geo_values on the same min and max
192+
# time_value index
189193
expect_identical(
190194
daily_edf %>%
191-
tidyr::complete(geo_value, time_value = full_seq(time_value, period = 1)),
192-
tibble::tibble(
193-
geo_value = c(1, 1, 2, 2),
194-
time_value = start_date + c(1, 2, 1, 2),
195-
value = c(1, NA, NA, 1)
195+
complete(geo_value, time_value = full_seq(time_value, period = 1)),
196+
tibble::tribble(
197+
~geo_value, ~time_value, ~value,
198+
1, start_date + 1, 1,
199+
1, start_date + 2, NA,
200+
1, start_date + 3, 3,
201+
2, start_date + 1, NA,
202+
2, start_date + 2, 2,
203+
2, start_date + 3, 3,
196204
) %>%
197205
as_epi_df(as_of = start_date + 3)
198206
)
207+
# Complete with grouping puts all the geo_values on individual min and max
208+
# time_value indices
199209
expect_identical(
200210
daily_edf %>%
201211
group_by(geo_value) %>%
202-
tidyr::complete(time_value = full_seq(time_value, period = 1)),
203-
daily_edf %>%
212+
complete(time_value = full_seq(time_value, period = 1)),
213+
tibble::tribble(
214+
~geo_value, ~time_value, ~value,
215+
1, start_date + 1, 1,
216+
1, start_date + 2, NA,
217+
1, start_date + 3, 3,
218+
2, start_date + 2, 2,
219+
2, start_date + 3, 3,
220+
) %>%
221+
as_epi_df(as_of = start_date + 3) %>%
204222
group_by(geo_value)
205223
)
206-
weekly_edf <- tibble::tibble(
207-
geo_value = c(1, 1, 1, 2),
208-
time_value = start_date + c(1, 15, 22, 1),
209-
value = 1
224+
# Complete has explicit=TRUE by default, but if it's FALSE, then complete only fills the implicit gaps
225+
# not those that are explicitly NA
226+
daily_edf <- tibble::tribble(
227+
~geo_value, ~time_value, ~value,
228+
1, start_date + 1, 1,
229+
1, start_date + 2, NA,
230+
1, start_date + 3, 3,
231+
2, start_date + 2, 2,
232+
2, start_date + 3, 3,
210233
) %>%
211234
as_epi_df(as_of = start_date + 3)
212235
expect_identical(
213-
weekly_edf %>%
214-
complete(geo_value,
215-
time_value = full_seq(time_value, period = 7),
216-
fill = list(value = 5)
217-
),
218-
tibble::tibble(
219-
geo_value = c(1, 1, 1, 1, 2, 2, 2, 2),
220-
time_value = start_date + c(1, 8, 15, 22, 1, 8, 15, 22),
221-
value = c(1, 5, 1, 1, 1, 5, 5, 5)
236+
daily_edf %>%
237+
complete(geo_value, time_value = full_seq(time_value, period = 1), fill = list(value = 0), explicit = FALSE),
238+
tibble::tribble(
239+
~geo_value, ~time_value, ~value,
240+
1, start_date + 1, 1,
241+
1, start_date + 2, NA,
242+
1, start_date + 3, 3,
243+
2, start_date + 1, 0,
244+
2, start_date + 2, 2,
245+
2, start_date + 3, 3,
222246
) %>%
223247
as_epi_df(as_of = start_date + 3)
224248
)
249+
# Complete works for weekly data and can take a fill value
250+
# No grouping
251+
weekly_edf <- tibble::tribble(
252+
~geo_value, ~time_value, ~value,
253+
1, start_date + 1, 1,
254+
1, start_date + 15, 3,
255+
2, start_date + 8, 2,
256+
2, start_date + 15, 3,
257+
) %>%
258+
as_epi_df(as_of = start_date + 3)
225259
expect_identical(
226260
weekly_edf %>%
227-
group_by(geo_value) %>%
228-
complete(
261+
complete(geo_value,
229262
time_value = full_seq(time_value, period = 7),
230-
fill = list(value = 5)
231-
) %>%
232-
ungroup(),
233-
tibble::tibble(
234-
geo_value = c(1, 1, 1, 1, 2),
235-
time_value = start_date + c(1, 8, 15, 22, 1),
236-
value = c(1, 5, 1, 1, 1)
263+
fill = list(value = 0)
264+
),
265+
tibble::tribble(
266+
~geo_value, ~time_value, ~value,
267+
1, start_date + 1, 1,
268+
1, start_date + 8, 0,
269+
1, start_date + 15, 3,
270+
2, start_date + 1, 0,
271+
2, start_date + 8, 2,
272+
2, start_date + 15, 3,
237273
) %>%
238274
as_epi_df(as_of = start_date + 3)
239275
)
276+
# With grouping
240277
expect_identical(
241278
weekly_edf %>%
242279
group_by(geo_value) %>%
243280
complete(
244-
time_value = full_seq(time_value, period = 1),
245-
fill = list(value = 5)
246-
) %>%
247-
ungroup(),
248-
tibble::tibble(
249-
geo_value = c(rep(1, 22), 2),
250-
time_value = start_date + c(1:22, 1),
251-
value = c(1, rep(5, 13), 1, rep(5, 6), 1, 1)
281+
time_value = full_seq(time_value, period = 7),
282+
fill = list(value = 0)
283+
),
284+
tibble::tribble(
285+
~geo_value, ~time_value, ~value,
286+
1, start_date + 1, 1,
287+
1, start_date + 8, 0,
288+
1, start_date + 15, 3,
289+
2, start_date + 8, 2,
290+
2, start_date + 15, 3,
252291
) %>%
253-
as_epi_df(as_of = start_date + 3)
292+
as_epi_df(as_of = start_date + 3) %>%
293+
group_by(geo_value)
254294
)
255295
})

0 commit comments

Comments
 (0)