Skip to contents

Stratifies data, builds one table per stratum with .tbl_fun, and stacks the tables with the strata as indented row headers nested inside one another. The interface is that of gtsummary::tbl_strata_nested_stack().

The header of the result comes from the first table, so its N often needs updating with modify_header(). The row headers live in rows whose hidden tbl_indent_id1 column is greater than zero; style them with, e.g., modify_bold(columns = label, rows = tbl_indent_id1 > 0). Rows with a missing value in any strata column are dropped with a message.

Usage

tbl_strata_nested_stack(
  data,
  strata,
  .tbl_fun,
  ...,
  row_header = "{strata}",
  quiet = FALSE
)

Arguments

data

(data.frame)
A data frame.

strata

(selector)
Columns of data to stratify by. Only observed combinations of their values appear. Strata columns cannot be named "stat", "stat_name", "n", "N", "p" or "strata".

.tbl_fun

(function or formula)
The table built within each stratum: a function of the stratum's data frame, or a one-sided formula where .x is the data frame.

...

Additional arguments passed to .tbl_fun.

row_header

(string)
The row header placed above each stratum's rows. The values {strata} (the stratum level), {n} (rows at the level), {N} (rows in the level's parent stratum) and {p} (n/N) are available in braces. Default is "{strata}".

quiet

(scalar logical)
Whether to suppress the message listing the column headers when they differ between the tables. Default is FALSE.

Value

A table of class c("tbl_strata_nested_stack", "tbl_stack", "ltsummary").

Examples

# Example 1 ----------------------------------
tbl_strata_nested_stack(
  trial,
  strata = trt,
  .tbl_fun = ~ .x |>
    tbl_summary(include = c(age, grade), missing = "no") |>
    modify_header(all_stat_cols() ~ "**Summary Statistics**")
)
# Example 2 ---------------------------------- # counts in the row headers, shown in bold tbl_strata_nested_stack( trial, strata = trt, .tbl_fun = ~ .x |> tbl_summary(include = c(age, grade), missing = "no") |> modify_header(all_stat_cols() ~ "**Summary Statistics**"), row_header = "{strata}, n={n}" ) |> modify_bold(columns = label, rows = tbl_indent_id1 > 0)