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.
incidence() objects are subclasses of tibbles they also have good integration with tidyverse verbs.
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()
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
inci %>% keep_first(3) inci %>% keep_last(3)
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())
We provide multiple accessors to easily access information about an
incidence() objects structure:
get_group_names()all return character vectors of the column names corresponding to the requested variables.
get_n()returns the number of observations.
get_interval()returns the interval of the object.
get_timespan()returns the number of days the object covers.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.