This function stratifies your data frame, builds gtsummary tables, and
stacks the resulting tables in a nested style. The underlying functionality
is similar to tbl_strata()
, except the resulting tables are nested or indented
within each group.
NOTE: The header from the first table is used for the final table. Oftentimes, this header will include incorrect Ns and must be updated.
tbl_strata_nested_stack(
data,
strata,
.tbl_fun,
...,
row_header = "{strata}",
quiet = FALSE
)
(data.frame
)
a data frame
(tidy-select
)
character vector or tidy-selector of columns in data to stratify results by.
Only observed combinations are shown in results.
(function
)
A function or formula. If a function, it is used as is.
If a formula, e.g. ~ .x %>% tbl_summary() %>% add_p()
, it is converted to a function.
The stratified data frame is passed to this function.
Additional arguments passed on to the .tbl_fun
function.
(string
)
string indicating the row headers that appear in the table.
The argument uses glue::glue()
syntax to insert values into the
row headers. Elements available to insert are strata
, n
, N
and p
.
The strata
element is the variable level of the strata variables.
Default is '{strata}'
.
(scalar logical
)
Logical indicating whether to suppress additional messaging. Default is FALSE
.
a stacked 'gtsummary' table
# 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**")
)
Characteristic
Summary Statistics1
1 Median (Q1, Q3); n (%)
# Example 2 ----------------------------------
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}"
) |>
# bold the row headers; print `x$table_body` to see hidden columns
modify_bold(columns = "label", rows = tbl_indent_id1 > 0)
Characteristic
Summary Statistics1
1 Median (Q1, Q3); n (%)