Column labels can be modified to include calculated statistics; e.g. the N can be dynamically included by wrapping it in curly brackets (following glue::glue syntax).

modify_header(
  x,
  update = NULL,
  stat_by = NULL,
  text_interpret = c("md", "html"),
  ...
)

Arguments

x

gtsummary object, e.g. tbl_summary or tbl_regression

update

list of formulas or a single formula specifying the updated column label. Columns from x$table_body may be selected.

stat_by

Used with tbl_summary(by=) objects with a by= argument. String specifying text to include above the summary statistics. The following fields are available for use in the headers:

  • {n} number of observations in each group,

  • {N} total number of observations,

  • {p} percentage in each group,

  • {level} the 'by' variable level,

Syntax follows glue::glue(), e.g. stat_by = "**{level}**, N = {n} ({style_percent(p)}%)".

text_interpret

String indicates whether text will be interpreted with gt::md() or gt::html(). Must be "md" (default) or "html".

...

Specify a column and updated column label, e.g. modify_header(p.value = "Model P-values"). This is provided as an alternative to the update= argument. They accomplish the same goal of updating column headers.

Value

Function return the same class of gtsummary object supplied

Example Output

Example 1

Example 2

See also

Author

Daniel D. Sjoberg

Examples

# create summary table tbl <- trial[c("age", "grade", "trt")] %>% tbl_summary(by = trt, missing = "no") %>% add_p() # print `.$table_body` to show column names and update headers tbl$table_body
#> # A tibble: 5 x 6 #> variable row_type label stat_1 stat_2 p.value #> <chr> <chr> <chr> <chr> <chr> <dbl> #> 1 age label Age, yrs 46 (37, 59) 48 (39, 56) 0.718 #> 2 grade label Grade NA NA 0.871 #> 3 grade level I 35 (36%) 33 (32%) NA #> 4 grade level II 32 (33%) 36 (35%) NA #> 5 grade level III 31 (32%) 33 (32%) NA
# Example 1 ---------------------------------- # updating column headers modify_header_ex1 <- tbl %>% modify_header( update = list( label ~ "**Variable**", p.value ~ "**P**" ) ) # Example 2 ---------------------------------- # using `stat_by=` argument to update headers modify_header_ex2 <- tbl %>% modify_header( stat_by = "**{level}**, N = {n} ({style_percent(p)}%)" )