nice_contingency_table: Create a Contingency Table with One or Two Summary Functions

View source: R/nice_contingency_table.R

nice_contingency_tableR Documentation

Create a Contingency Table with One or Two Summary Functions

Description

This function creates a contingency table by grouping the data into unique levels of rows and columns, then applies one or more summary functions (by default mean and a count via length) to summarize the variable of interest (var). Labels can be assigned manually or automatically retrieved from variable attributes if available.

Usage

nice_contingency_table(
  rows,
  columns,
  var,
  fn = list(Mean = mean, n = function(.) length(.)),
  auto_labels = TRUE,
  var_label = NULL,
  rows_label = NULL,
  columns_label = NULL,
  fn_label = NULL,
  title = NULL,
  footnote = NULL,
  file = NULL,
  remove_missing = TRUE,
  sort = TRUE,
  ...
)

Arguments

rows

A vector or factor for the row grouping variable. If columns and var are missing, rows can be a list where its first element is the row variable, second is the column variable, and third is the variable of interest.

columns

A vector or factor for the column grouping variable. If missing, it will be extracted from the second element of rows (if rows is a list).

var

A numeric vector containing the values to be summarized. If missing, it will be extracted from the third element of rows (if rows is a list).

fn

A named list of functions to apply to var for each combination of rows and columns. Defaults to list(Mean = mean, n = \(.) length(.)). If only one function is provided, the table will display that result. If two are provided, the results will be concatenated (e.g., "Mean (n)").

auto_labels

A logical value indicating whether to automatically retrieve labels from attributes of rows, columns, or var. Defaults to TRUE.

var_label

An optional character string specifying the label for var.

rows_label

An optional character string specifying the label for rows.

columns_label

An optional character string specifying the label for columns.

fn_label

An optional character vector specifying the labels for the functions listed in fn. If not provided, defaults to the names of fn.

title

An optional character string specifying the title of the table. By default, it is constructed from fn_label and var_label.

footnote

An optional character string specifying a footnote for the table.

file

An optional path to a file where the table output should be saved.

remove_missing

A logical value indicating whether rows with missing values (NA) in var should be removed. Defaults to TRUE.

sort

A logical value indicating whether to sort the unique levels of rows and columns. Defaults to TRUE.

...

Additional arguments passed to nice_table.

Details

If rows, columns, and var are provided separately, the function will group the data by rows and columns, then apply the functions in fn to var within each combination of row and column levels.

If columns and var are not specified, you can instead provide a list to rows whose elements are (1) the row variable, (2) the column variable, and (3) the variable to be summarized.

If two functions are provided via fn, the resulting table will show the first function's value followed by the second function's value in parentheses. For example, if the first function is mean and the second is a count function, a cell might display "3.45 (10)".

The function attempts to automatically retrieve labels from the data if auto_labels = TRUE and the variables have "label" attributes. If labels are not found or if var_label, rows_label, or columns_label is explicitly specified, those provided labels will be used.

Any NA values in var are removed before performing calculations if remove_missing = TRUE.

By default, the function also sorts the levels of the row and column variables.

Value

An html table.

Examples

nice_contingency_table(wmisc:::mtcars_labeled[, c("cyl", "carb", "mpg")])

nice_contingency_table(
  wmisc:::mtcars_labeled[, c("cyl", "am", "mpg")],
  fn = list(Median = median, "mean average deviation" = mad),
  label_na = "-",
)


jazznbass/wmisc documentation built on Jan. 28, 2025, 11:13 p.m.