Learn R Programming

SimplifyStats (version 2.0.4)

add_missing: Add rows for unused combinations of factor levels

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

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

Run this code
# NOT RUN {
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))
# }

Run the code above in your browser using DataLab