Skip to contents

Adds or modifies labels describing the summary statistics presented for each variable in a tbl_summary() table, for example "Age, Median (Q1, Q3)". Because the table then carries this information, the footnote describing the statistics is removed.

Usage

add_stat_label(x, location = c("row", "column"), label = NULL, ...)

Arguments

x

(tbl_summary)
A table created with tbl_summary().

location

(string)
Where the statistic label goes: "row" (the default) appends it to the variable label row, and "column" adds a column with the statistic labels.

label

(formula-list)
Updates to the statistic labels, e.g. label = all_categorical() ~ "No. (%)". When not specified, the default labels are used. For "continuous2" variables, pass one label per statistic line.

...

Not used.

Value

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

Tips

add_stat_label(location = "row") works by adding a column named "stat_label" to x$table_body and merging it into the "label" column when the table is printed, so the label column itself is unchanged. Code that matches on x$table_body$label keeps working.

The new column needs a default label for categorical variables, which is "n (%)". It can be replaced with any text, or blanked with NA_character_; blanking is useful with location = "row" to keep the output for categorical variables identical to the table without add_stat_label().

Examples

tbl <- trial |>
  tbl_summary(by = trt, include = c(age, grade, response))

# Example 1 ----------------------------------
# add the statistic to the variable label row
tbl |>
  add_stat_label(
    # update default statistic label for continuous variables
    label = all_continuous() ~ "med. (iqr)"
  )
# Example 2 ---------------------------------- # add a new column with the statistic labels tbl |> add_stat_label(location = "column")
# Example 3 ---------------------------------- # one label per line of a continuous2 variable trial |> tbl_summary( by = trt, include = c(age, grade), type = all_continuous() ~ "continuous2", statistic = all_continuous() ~ c("{median} ({p25}, {p75})", "{min} - {max}") ) |> add_stat_label(label = age ~ c("IQR", "Range"))