add_missing: Add rows for unused combinations of factor levels

Description Usage Arguments Value Examples

View source: R/add_missing.R

Description

add_missing adds rows for unused combinations of factor levels.

The function takes as input a data.frame or tibble, the column names of grouping variables, and a named list of default values.

Usage

1
add_missing(x, group_cols, defaults)

Arguments

x

A data.frame or tibble.

group_cols

Vector of the names of the grouping columns.

defaults

A named list of default values.

Value

A tibble

A tibble or data.frame, depending on the class of x.

Examples

1
2
3
4
5
iris_sub <- dplyr::filter(iris, Species != "virginica")
iris_summary <- dplyr::group_by(iris_sub, Species)
iris_summary <- dplyr::summarise(iris_summary, N = dplyr::n())
iris_summary <- dplyr::ungroup(iris_summary)
add_missing(iris_summary, "Species", list(N = 0))

Example output

`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 2
  Species        N
  <fct>      <dbl>
1 setosa        50
2 versicolor    50
3 virginica      0

SimplifyStats documentation built on April 14, 2020, 6:27 p.m.