dplyr-verbs: dplyr and tidyr verbs

dplyr-verbsR Documentation

dplyr and tidyr verbs

Description

dplyr and tidyr methods that implicitly account for the inherent grouping structure of incidence2 objects.

Usage

## S3 method for class 'incidence2'
mutate(
  .data,
  ...,
  .by,
  .keep = c("all", "used", "unused", "none"),
  .before = NULL,
  .after = NULL
)

## S3 method for class 'incidence2'
nest(.data, ..., .by, .key, .names_sep)

## S3 method for class 'incidence2'
summarise(.data, ..., .by, .groups)

Arguments

.data

An incidence2 object.

...

Only used by mutate() and summarise() and, in which case, passed to underlying dplyr function.

.by

Not used as grouping structure implicit.

.keep

Control which columns from .data are retained in the output. Grouping columns and columns created by ... are always kept.

  • "all" retains all columns from .data. This is the default.

  • "used" retains only the columns used in ... to create new columns. This is useful for checking your work, as it displays inputs and outputs side-by-side.

  • "unused" retains only the columns not used in ... to create new columns. This is useful if you generate new columns, but no longer need the columns used to generate them.

  • "none" doesn't retain any extra columns from .data. Only the grouping variables and columns created by ... are kept.

.before, .after

<tidy-select> Optionally, control where new columns should appear (the default is to add to the right hand side). See relocate() for more details.

.key

The name of the resulting nested column. Only applicable when ... isn't specified, i.e. in the case of df %>% nest(.by = x).

If NULL, then "data" will be used by default.

.names_sep

Not used.

.groups

Not used.

Value

  • For mutate() a modified incidence2 object if the necessary invariants are preserved, otherwise a tibble.

  • For nest() a nested tibble with rows corresponding to the count variable and (optionally) group columns of the input object.

  • For summarise a tibble with rows corresponding to the underlying groupings. The columns are a combination of the grouping keys and the summary expressions provided.

See Also

dplyr::mutate, tidyr::nest and dplyr::summarise for the underlying generics.

Examples


if (requireNamespace("outbreaks", quietly = TRUE) && requireNamespace("ggplot2", quietly = TRUE)) {
    data(ebola_sim_clean, package = "outbreaks")
    x <- subset(ebola_sim_clean$linelist, !is.na(hospital))
    dat <- incidence_(x, date_of_onset, hospital, interval = "isoweek")

    mutate(dat, ave = data.table::frollmean(count, n = 3L, align = "right")) |>
        plot(border_colour = "white", angle = 45) +
        ggplot2::geom_line(ggplot2::aes(x = date_index, y = ave))

    nest(dat)

    summarise(dat, model = list(glm(count ~ date_index, family = "poisson")))
}



incidence2 documentation built on April 15, 2025, 1:10 a.m.