Skip to contents

keep_first() and keep_last() keep the first and last n rows to occur for each grouping when in ascending date order. keep_peaks() keeps the rows with the maximum count value for each group.

Usage

keep_first(x, n, complete_dates = TRUE, ...)

keep_last(x, n, complete_dates = TRUE, ...)

keep_peaks(x, complete_dates = TRUE, first_only = FALSE, ...)

Arguments

x

<incidence2> object.

n

[integer]

Number of entries to keep.

double vectors will be converted via as.integer(n).

complete_dates

[bool]

Should complete_dates() be called on the data prior to keeping the first entries.

Defaults to TRUE.

...

Other arguments passed to complete_dates().

first_only

[bool]

Should only the first peak (by date) be kept.

Defaults to TRUE.

Value

Incidence object with the chosen entries.

Examples

data.table::setDTthreads(2)
if (requireNamespace("outbreaks", quietly = TRUE)) {
withAutoprint({
    data(ebola_sim_clean, package = "outbreaks")
    dat <- ebola_sim_clean$linelist
    inci <- incidence(dat, "date_of_onset")
    keep_first(inci, 3)
    keep_last(inci, 3)
})
}
#> > data(ebola_sim_clean, package = "outbreaks")
#> > dat <- ebola_sim_clean$linelist
#> > inci <- incidence(dat, "date_of_onset")
#> > keep_first(inci, 3)
#> # incidence:  3 x 3
#> # count vars: date_of_onset
#>   date_index count_variable count
#> * <date>     <chr>          <int>
#> 1 2014-04-07 date_of_onset      1
#> 2 2014-04-08 date_of_onset      0
#> 3 2014-04-09 date_of_onset      0
#> > keep_last(inci, 3)
#> # incidence:  3 x 3
#> # count vars: date_of_onset
#>   date_index count_variable count
#> * <date>     <chr>          <int>
#> 1 2015-04-28 date_of_onset      8
#> 2 2015-04-29 date_of_onset      2
#> 3 2015-04-30 date_of_onset      2