View source: R/add_stat_label.R
add_stat_label | R Documentation |
Adds or modifies labels describing the summary statistics presented for
each variable in a tbl_summary()
table.
add_stat_label(x, ...)
## S3 method for class 'tbl_summary'
add_stat_label(x, location = c("row", "column"), label = NULL, ...)
## S3 method for class 'tbl_svysummary'
add_stat_label(x, location = c("row", "column"), label = NULL, ...)
## S3 method for class 'tbl_ard_summary'
add_stat_label(x, location = c("row", "column"), label = NULL, ...)
x |
( |
... |
These dots are for future extensions and must be empty. |
location |
( |
label |
( |
A tbl_summary
or tbl_svysummary
object
When using add_stat_label(location='row')
with subsequent tbl_merge()
,
it's important to have somewhat of an understanding of the underlying
structure of the gtsummary table.
add_stat_label(location='row')
works by adding a new column called
"stat_label"
to x$table_body
. The "label"
and "stat_label"
columns are merged when the gtsummary table is printed.
The tbl_merge()
function merges on the "label"
column (among others),
which is typically the first column you see in a gtsummary table.
Therefore, when you want to merge a table that has run add_stat_label(location='row')
you need to match the "label"
column values before the "stat_column"
is merged with it.
For example, the following two tables merge properly
tbl1 <- trial %>% select(age, grade) |> tbl_summary() |> add_stat_label() tbl2 <- lm(marker ~ age + grade, trial) |> tbl_regression() tbl_merge(list(tbl1, tbl2))
The addition of the new "stat_label"
column requires a default
labels for categorical variables, which is "No. (%)"
. This
can be changed to either desired text or left blank using NA_character_
.
The blank option is useful in the location="row"
case to keep the
output for categorical variables identical what was produced without
a "add_stat_label()"
function call.
Daniel D. Sjoberg
tbl <- trial |>
dplyr::select(trt, age, grade, response) |>
tbl_summary(by = trt)
# Example 1 ----------------------------------
# Add statistic presented to the variable label row
tbl |>
add_stat_label(
# update default statistic label for continuous variables
label = all_continuous() ~ "med. (iqr)"
)
# Example 2 ----------------------------------
tbl |>
add_stat_label(
# add a new column with statistic labels
location = "column"
)
# Example 3 ----------------------------------
trial |>
select(age, grade, trt) |>
tbl_summary(
by = trt,
type = all_continuous() ~ "continuous2",
statistic = all_continuous() ~ c("{median} ({p25}, {p75})", "{min} - {max}"),
) |>
add_stat_label(label = age ~ c("IQR", "Range"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.