Skip to content

Commit cfeda76

Browse files
authored
Merge pull request #488 from cmu-delphi/lcb/complete-grouped-epi_dfs
Add `complete.epi_df` method so grouped completions don't drop epi_df…
2 parents 62155cc + 4335dc1 commit cfeda76

13 files changed

+312
-17
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: epiprocess
33
Title: Tools for basic signal processing in epidemiology
4-
Version: 0.8.0
4+
Version: 0.8.1
55
Authors@R: c(
66
person("Jacob", "Bien", role = "ctb"),
77
person("Logan", "Brooks", email = "[email protected]", role = c("aut", "cre")),

NAMESPACE

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ S3method(as_tsibble,epi_df)
1111
S3method(autoplot,epi_df)
1212
S3method(clone,epi_archive)
1313
S3method(clone,grouped_epi_archive)
14+
S3method(complete,epi_df)
1415
S3method(dplyr_col_modify,col_modify_recorder_df)
1516
S3method(dplyr_col_modify,epi_df)
1617
S3method(dplyr_reconstruct,epi_df)
@@ -50,6 +51,7 @@ export(as_epi_df)
5051
export(as_tsibble)
5152
export(autoplot)
5253
export(clone)
54+
export(complete)
5355
export(detect_outlr)
5456
export(detect_outlr_rm)
5557
export(detect_outlr_stl)
@@ -64,6 +66,7 @@ export(epix_merge)
6466
export(epix_slide)
6567
export(epix_truncate_versions_after)
6668
export(filter)
69+
export(full_seq)
6770
export(geo_column_names)
6871
export(group_by)
6972
export(group_modify)
@@ -194,6 +197,8 @@ importFrom(stats,median)
194197
importFrom(tibble,as_tibble)
195198
importFrom(tibble,new_tibble)
196199
importFrom(tibble,validate_tibble)
200+
importFrom(tidyr,complete)
201+
importFrom(tidyr,full_seq)
197202
importFrom(tidyr,unnest)
198203
importFrom(tidyselect,any_of)
199204
importFrom(tidyselect,eval_select)

NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Pre-1.0.0 numbering scheme: 0.x will indicate releases, while 0.x.y will indicat
44

55
# epiprocess 0.9
66

7+
## Improvements
8+
9+
- Added `complete.epi_df`, which fills in missing values in an `epi_df` with
10+
`NA`s. Uses `tidyr::complete` underneath and preserves `epi_df` metadata.
11+
712
# epiprocess 0.8
813

914
## Breaking changes

R/epi_df.R

+2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ new_epi_df <- function(x = tibble::tibble(), geo_type, time_type, as_of,
180180
}
181181

182182
#' @rdname epi_df
183+
#' @param ... used for specifying column names, as in [`dplyr::rename`]. For
184+
#' example, `geo_value = STATEFP, time_value = end_date`.
183185
#' @export
184186
as_epi_df <- function(x, ...) {
185187
UseMethod("as_epi_df")

R/methods-epi_df.R

+74-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#' use `attr(your_epi_df, "decay_to_tibble") <- FALSE` beforehand.
99
#'
1010
#' @template x
11-
#' @param ... additional arguments to forward to `NextMethod()`
11+
#'
12+
#' @inheritParams tibble::as_tibble
1213
#'
1314
#' @importFrom tibble as_tibble
1415
#' @export
@@ -48,9 +49,9 @@ as_tsibble.epi_df <- function(x, key, ...) {
4849
#' Print and summary functions for an `epi_df` object.
4950
#'
5051
#' @template x
51-
#' @param ... additional arguments to forward to `NextMethod()`
5252
#'
5353
#' @method print epi_df
54+
#' @param ... additional arguments to forward to `NextMethod()`, or unused
5455
#' @export
5556
print.epi_df <- function(x, ...) {
5657
cat(
@@ -76,7 +77,6 @@ print.epi_df <- function(x, ...) {
7677
#' Currently unused.
7778
#'
7879
#' @method summary epi_df
79-
#' @rdname print.epi_df
8080
#' @importFrom rlang .data
8181
#' @importFrom stats median
8282
#' @export
@@ -241,6 +241,77 @@ group_modify.epi_df <- function(.data, .f, ..., .keep = FALSE) {
241241
dplyr::dplyr_reconstruct(NextMethod(), .data)
242242
}
243243

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+
#'
250+
#' @param data an `epi_df`
251+
#' @param ... see [`tidyr::complete`]
252+
#' @param fill see [`tidyr::complete`]
253+
#' @param explicit see [`tidyr::complete`]
254+
#'
255+
#' @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))
306+
#' @export
307+
complete.epi_df <- function(data, ..., fill = list(), explicit = TRUE) {
308+
result <- dplyr::dplyr_reconstruct(NextMethod(), data)
309+
if ("time_value" %in% names(rlang::call_match(dots_expand = FALSE)[["..."]])) {
310+
attr(result, "metadata")$time_type <- guess_time_type(result$time_value)
311+
}
312+
result
313+
}
314+
244315
#' @method unnest epi_df
245316
#' @rdname print.epi_df
246317
#' @param data an `epi_df`

R/reexports.R

+13
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ dplyr::slice
5757
tidyr::unnest
5858

5959

60+
#' @importFrom tidyr complete
61+
#' @export
62+
tidyr::complete
63+
64+
# We don't provide a method for full_seq, but complete-ing using
65+
# full_seq(time_value) is still needed to make some downstream things behave
66+
# nicely. So make that more ergonomic/discoverable with a re-export:
67+
68+
#' @importFrom tidyr full_seq
69+
#' @export
70+
tidyr::full_seq
71+
72+
6073
# ggplot2 -----------------------------------------------------------------
6174

6275
#' @importFrom ggplot2 autoplot

man/as_tibble.epi_df.Rd

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

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/epi_df.Rd

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

man/print.epi_df.Rd

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

man/reexports.Rd

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

man/summary.epi_df.Rd

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

0 commit comments

Comments
 (0)