View source: R/nice_contingency_table.R
nice_contingency_table | R Documentation |
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.
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,
...
)
rows |
A vector or factor for the row grouping variable. If |
columns |
A vector or factor for the column grouping variable. If missing,
it will be extracted from the second element of |
var |
A numeric vector containing the values to be summarized. If missing,
it will be extracted from the third element of |
fn |
A named list of functions to apply to |
auto_labels |
A logical value indicating whether to automatically retrieve
labels from attributes of |
var_label |
An optional character string specifying the label for
|
rows_label |
An optional character string specifying the label for
|
columns_label |
An optional character string specifying the label for
|
fn_label |
An optional character vector specifying the labels for
the functions listed in |
title |
An optional character string specifying the title of the table.
By default, it is constructed from |
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 ( |
sort |
A logical value indicating whether to sort the unique levels of
|
... |
Additional arguments passed to |
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.
An html table.
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 = "-",
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.