Handling incidence objects

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.align = "center",
  fig.width = 7,
  fig.height = 5
)

incidence() objects are easy to work with, and we providing helper functions for both manipulating and accessing the underlying data and attributes. As incidence() objects are subclasses of tibbles they also have good integration with tidyverse verbs.

Modifying incidence objects

regroup()

Sometimes you may find you've created a grouped incidence but now want to change the internal grouping. Assuming you are after a subset of the grouping already generated, then you can use to regroup() function to get the desired aggregation:

library(outbreaks)
library(dplyr)
library(incidence2)

# load data
dat <- ebola_sim_clean$linelist

# generate the incidence object with 3 groups
inci <- incidence(dat, date_of_onset, groups = c(gender, hospital, outcome), interval = "week")
inci

# regroup to just two groups
inci %>% regroup(c(gender, outcome))

# drop all groups
inci %>% regroup()

keep_first() and keep_last()

Once your data is grouped by date, you may want to select the first or last few entries based on a particular date grouping using keep_first() and keep_last():

inci %>% keep_first(3)
inci %>% keep_last(3)

Tidyverse compatibility

incidence2 has been written with tidyverse compatibility (in particular dplyr) at the forefront of the design choices we have made. By this we mean that if an operation from dplyr is applied to an incidence object then as long as the invariants of the object are preserved (i.e. groups, interval and uniqueness of rows) then the object returned will be an incidence object. If the invariants are not preserved then a tibble will be returned instead.

library(dplyr)

# create incidence object
inci <- incidence(dat, date_of_onset, interval = "week", groups = c(hospital, gender))

# filtering preserves class
inci %>%  filter(gender == "f", hospital == "Rokupa Hospital")

# slice operations preserve class
inci %>% slice_sample(n = 10)

inci %>%  slice(1, 5, 10)

# mutate preserve class
inci %>%  mutate(future = date_index + 999)

# rename preserve class
inci %>%  rename(left_bin = date_index)

# select returns a tibble unless all date, count and group variables are preserved
inci %>% select(-1)

inci %>% select(everything())

Accessing variable information

We provide multiple accessors to easily access information about an incidence() objects structure:



Try the incidence2 package in your browser

Any scripts or data that you put into this service are public.

incidence2 documentation built on July 15, 2021, 1:06 a.m.