nice_contingency_table.RdThis 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.
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.
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).
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).
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)").
A logical value indicating whether to automatically retrieve
labels from attributes of rows, columns, or var.
Defaults to TRUE.
An optional character string specifying the label for
var.
An optional character string specifying the label for
rows.
An optional character string specifying the label for
columns.
An optional character vector specifying the labels for
the functions listed in fn. If not provided, defaults to the names
of fn.
An optional character string specifying the title of the table.
By default, it is constructed from fn_label and var_label.
An optional character string specifying a footnote for the table.
An optional path to a file where the table output should be saved.
A logical value indicating whether rows with missing
values (NA) in var should be removed. Defaults to TRUE.
A logical value indicating whether to sort the unique levels of
rows and columns. Defaults to TRUE.
Additional arguments passed to nice_table.
An html table.
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.
nice_contingency_table(wmisc:::mtcars_labeled[, c("cyl", "carb", "mpg")])
Table
Mean (n) table for ‘Miles/(US) gallon’
1
2
3
4
6
8
nice_contingency_table(
wmisc:::mtcars_labeled[, c("cyl", "am", "mpg")],
fn = list(Median = median, "mean average deviation" = mad),
label_na = "-",
)
Table
Median (mean average deviation) table for ‘Miles/(US) gallon’
0
1