Skip to contents

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().

Usage

tbl_cross(
  data,
  row = 1L,
  col = 2L,
  label = NULL,
  statistic = ifelse(percent == "none", "{n}", "{n} ({p}%)"),
  digits = NULL,
  percent = c("none", "column", "row", "cell"),
  margin = c("column", "row"),
  missing = c("ifany", "always", "no"),
  missing_text = "Unknown",
  margin_text = "Total"
)

Arguments

data

(data.frame)
A data frame.

row

(selector)
The column of data shown in the rows. Default is the first column of data.

col

(selector)
The column of data shown across the columns. Default is the second column of data. Specify both row and col, or neither.

label

(formula-list)
Labels for the row and col variables, e.g. list(trt = "Treatment"). The default for each is its label attribute, 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}%)" when percent is "column", "row" or "cell".

digits

(integer, function or list)
How the statistics are rounded, passed on as tbl_summary(digits = everything() ~ digits). By default counts are shown without decimals and percentages are formatted with style_percent(). For the statistic "{n} ({p}%)" with the percentage rounded to two decimal places use digits = c(0, 2); a formatting function such as style_sigfig is 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. Use margin = NULL for neither.

missing

(string)
How missing values are shown: "ifany" (the default) adds a level named missing_text to a variable that has missing values, "always" adds the level whether or not there are missing values, and "no" drops the observations with a missing row or col value.

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".

Value

A table of class c("tbl_cross", "tbl_summary", "ltsummary").

See also

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.