Creates a cross table of two categorical variables: the levels of row
down the rows, the levels of col across the columns, and margin totals.
The table is built with tbl_summary(), so add_p(), inline_text(),
bold_labels() and the modifier functions work on it. The interface is
that of gtsummary::tbl_cross().
Arguments
- data
(
data.frame)
A data frame.- row
(selector)
The column ofdatashown in the rows. Default is the first column ofdata.- col
(selector)
The column ofdatashown across the columns. Default is the second column ofdata. Specify bothrowandcol, or neither.- label
(formula-list)
Labels for therowandcolvariables, e.g.list(trt = "Treatment"). The default for each is itslabelattribute, or the column name when there is none.- statistic
(
string)
The statistic shown in each cell, with statistic names in curly braces. Default is"{n}", or"{n} ({p}%)"whenpercentis"column","row"or"cell".- digits
(
integer,functionorlist)
How the statistics are rounded, passed on astbl_summary(digits = everything() ~ digits). By default counts are shown without decimals and percentages are formatted withstyle_percent(). For the statistic"{n} ({p}%)"with the percentage rounded to two decimal places usedigits = c(0, 2); a formatting function such asstyle_sigfigis also accepted.- percent
(
string)
The percentages shown alongside the counts:"none"(the default),"column","row"or"cell".- margin
(
character)
The margins added to the table:"column"for a total column,"row"for a total row, or both, the default. Usemargin = NULLfor neither.- missing
(
string)
How missing values are shown:"ifany"(the default) adds a level namedmissing_textto a variable that has missing values,"always"adds the level whether or not there are missing values, and"no"drops the observations with a missingroworcolvalue.- missing_text
(
string)
The label of the missing-value level. Default is"Unknown".- margin_text
(
string)
The label of the margin totals. Default is"Total".
See also
add_p_cross for the p-value comparing the two variables,
inline_text() for reporting cells in text.
Other cross tables:
add_p_cross
Other table builders:
tbl_continuous(),
tbl_custom_summary(),
tbl_likert(),
tbl_merge(),
tbl_regression(),
tbl_stack(),
tbl_strata(),
tbl_strata_nested_stack(),
tbl_summary(),
tbl_survfit(),
tbl_uvregression(),
tbl_wide_summary()
Examples
# Example 1 ----------------------------------
trial |>
tbl_cross(row = trt, col = response) |>
bold_labels()
# Example 2 ----------------------------------
trial |>
tbl_cross(row = stage, col = trt, percent = "cell") |>
add_p() |>
bold_labels()
# Example 3 ----------------------------------
# no margins, missing observations dropped, row percentages
trial |>
tbl_cross(row = stage, col = response, margin = NULL, missing = "no", percent = "row")
#> 7 observations with missing data have been removed.