contingency_table_by: Create data frame formed like a contingency-table

View source: R/dplyr-like.R

contingency_table_byR Documentation

Create data frame formed like a contingency-table

Description

Counts by the specified two variables and the pivots the count data frame wider to a two-dimensional contingency table. Please note that the resulting data frame is suitable for convenient output or use with functions that work on matrix-like data, but does not fulfill the tidy data criteria.

Usage

contingency_table_by(.tbl, var1, var2, na.rm = F, add_margins = F)

Arguments

.tbl

A data frame

var1

First column to count by

var2

Second column to count by

na.rm

Shall NA values be removed prior to counting?

add_margins

Add row- and column wise margins as extra column and row

Value

A data frame

Examples

library(magrittr)
if (requireNamespace("datasets", quietly = TRUE))
{
   mtcars %>% contingency_table_by(cyl, gear)
}

tidytidbits documentation built on March 18, 2022, 6:10 p.m.