R/datasummary_crosstab.R
datasummary_crosstab.Rd
Convenience function to tabulate counts, cell percentages, and row/column percentages for categorical variables. See the Details section for a description of the internal design. For more complex cross tabulations, use datasummary directly.
datasummary_crosstab( formula, statistic = 1 ~ 1 + N + Percent("row"), data, output = "default", fmt = 1, title = NULL, notes = NULL, align = NULL, add_columns = NULL, add_rows = NULL, sparse_header = TRUE, escape = TRUE, ... )
formula  A twosided formula to describe the table: rows ~ columns,
where rows and columns are variables in the data. Rows and columns may
contain interactions, e.g., 

statistic  A formula of the form 
data  A data.frame (or tibble) 
output  filename or object type (character string)

fmt  determines how to format numeric values

title  string 
notes  list or vector of notes to append to the bottom of the table. 
align  A string with a number of characters equal to the number of columns in
the table (e.g.,

add_columns  a data.frame (or tibble) with the same number of rows as your main table. 
add_rows  a data.frame (or tibble) with the same number of columns as your main table. By default, rows are appended to the bottom of the table. You can define a "position" attribute of integers to set the row positions. See Examples section below. 
sparse_header  TRUE or FALSE. TRUE eliminates column headers which
have a unique label across all columns, except for the row immediately above
the data. FALSE keeps all headers. The order in which terms are entered in
the formula determines the order in which headers appear. For example,

escape  boolean TRUE escapes or substitutes LaTeX/HTML characters which could prevent the file from compiling/displaying. This setting does not affect captions or notes. 
...  all other arguments are passed through to the
tablemaking functions. This allows users to pass arguments directly to

datasummary_crosstab
is a wrapper around the datasummary
function. This wrapper works by creating a customized formula and by
feeding it to datasummary
. The customized formula comes in two parts.
First, we take a twosided formula supplied by the formula
argument.
All variables of that formula are wrapped in a Factor()
call to ensure
that the variables are treated as categorical.
Second, the statistic
argument gives a twosided formula which specifies
the statistics to include in the table. datasummary_crosstab
modifies
this formula automatically to include "clean" labels.
Finally, the formula
and statistic
formulas are combined into a single
formula which is fed directly to the datasummary
function to produce the
table.
Variables in formula
are automatically wrapped in Factor()
.
The behavior of modelsummary
can be affected by setting global options:
modelsummary_factory_default
modelsummary_factory_latex
modelsummary_factory_html
modelsummary_factory_png
modelsummary_get
modelsummary_format_numeric_latex
modelsummary_format_numeric_html
modelsummary
supports 4 tablemaking packages: kableExtra
, gt
,
flextable
, and huxtable
. Some of these packages have overlapping
functionalities. For example, 3 of those packages can export to LaTeX. To
change the default backend used for a specific file format, you can use
the options
function:
options(modelsummary_factory_html = 'kableExtra')
options(modelsummary_factory_latex = 'gt')
options(modelsummary_factory_word = 'huxtable')
options(modelsummary_factory_png = 'gt')
modelsummary
can use two sets of packages to extract information from
statistical models: broom
and the easystats
family (performance
and
parameters
). By default, it uses broom
first and easystats
as a
fallback if broom
fails. You can change the order of priorities
or include goodnessoffit extracted by both packages by setting:
options(modelsummary_get = "broom")
options(modelsummary_get = "easystats")
options(modelsummary_get = "all")
By default, LaTeX tables enclose all numeric entries in the \num{}
command
from the siunitx package. To prevent this behavior, or to enclose numbers
in dollar signs (for LaTeX math mode), users can call:
options(modelsummary_format_numeric_latex = "plain")
options(modelsummary_format_numeric_latex = "mathmode")
A similar option can be used to display numerical entries using MathJax in HTML tables:
options(modelsummary_format_numeric_html = "mathjax")
if (FALSE) { # crosstab of two variables, showing counts, row percentages, and row/column totals datasummary_crosstab(cyl ~ gear, data = mtcars) # crosstab of two variables, showing counts only and no totals datasummary_crosstab(cyl ~ gear, statistic = ~ N, data = mtcars) # crosstab of three variables datasummary_crosstab(am * cyl ~ gear, data = mtcars) # crosstab with two variables and column percentages datasummary_crosstab(am ~ gear, statistic = ~ Percentage("col"), data = mtcars) }