Skip to contents

This is a thin wrapper of gtsummary::tbl_summary(). The function is meant for 'continuous2' and 'categorical' summary types only.

  • Continuous variables default to type 'continuous2'.

  • The number of non-missing is placed on the first row for all summaries.

  • The default formatting function for percentages is gtsummary::label_style_number(digits = 1, scale = 100).

  • The gtsummary::add_stat_label() is run automatically after the function's execution.

Usage

tbl_demographics(
  data,
  by = NULL,
  label = NULL,
  statistic = list(all_continuous() ~ c("{mean} ({sd})", "{median} ({p25}, {p75})",
    "{min}, {max}"), all_categorical() ~ "{n} ({p}%)"),
  digits = NULL,
  type = NULL,
  value = NULL,
  sort = all_categorical(FALSE) ~ "alphanumeric",
  percent = c("column", "row", "cell"),
  include = everything()
)

Arguments

data

(data.frame)
A data frame.

by

(tidy-select)
A single column from data. Summary statistics will be stratified by this variable. Default is NULL.

label

(formula-list-selector)
Used to override default labels in summary table, e.g. list(age = "Age, years"). The default for each variable is the column label attribute, attr(., 'label'). If no label has been set, the column name is used.

statistic

(formula-list-selector)
Specifies summary statistics to display for each variable. The default is list(all_continuous() ~ "{median} ({p25}, {p75})", all_categorical() ~ "{n} ({p}%)"). See below for details.

digits

(formula-list-selector)
Specifies how summary statistics are rounded. Values may be either integer(s) or function(s). If not specified, default formatting is assigned via assign_summary_digits(). See below for details.

type

(formula-list-selector)
Specifies the summary type. Accepted value are c("continuous", "continuous2", "categorical", "dichotomous"). If not specified, default type is assigned via assign_summary_type(). See below for details.

value

(formula-list-selector)
Specifies the level of a variable to display on a single row. The gtsummary type selectors, e.g. all_dichotomous(), cannot be used with this argument. Default is NULL. See below for details.

sort

(formula-list-selector)
Specifies sorting to perform for categorical variables. Values must be one of c("alphanumeric", "frequency"). Default is all_categorical(FALSE) ~ "alphanumeric".

percent

(string)
Indicates the type of percentage to return. Must be one of c("column", "row", "cell"). Default is "column".

include

(tidy-select)
Variables to include in the summary table. Default is everything().

Value

a 'gtsummary' table

Examples

theme_gtsummary_roche()
#> Setting theme "Roche"
tbl <-
  trial |>
  tbl_demographics(
    by = trt,
    include = c(age, grade)
  )
#> Setting theme "Roche"

tbl
Characteristic Drug A
N = 98
Drug B
N = 102
Age

    n 91 98
    Mean (SD) 47 (15) 47 (14)
    Median (Q1, Q3) 46 (37, 60) 48 (39, 56)
    Min, Max 6, 78 9, 83
Grade, n (%)

    n 98 102
    I 35 (35.7%) 33 (32.4%)
    II 32 (32.7%) 36 (35.3%)
    III 31 (31.6%) 33 (32.4%)
# extract ARD from table gather_ard(tbl) #> $tbl_summary #> {cards} data frame: 66 x 12 #> group1 group1_level variable variable_level stat_name stat_label stat #> 1 trt Drug A grade I n n 35 #> 2 trt Drug A grade I N N 98 #> 3 trt Drug A grade I p % 0.357 #> 4 trt Drug B grade I n n 33 #> 5 trt Drug B grade I N N 102 #> 6 trt Drug B grade I p % 0.324 #> 7 trt Drug A grade II n n 32 #> 8 trt Drug A grade II N N 98 #> 9 trt Drug A grade II p % 0.327 #> 10 trt Drug B grade II n n 36 #> gts_column #> 1 stat_1 #> 2 stat_1 #> 3 stat_1 #> 4 stat_2 #> 5 stat_2 #> 6 stat_2 #> 7 stat_1 #> 8 stat_1 #> 9 stat_1 #> 10 stat_2 #> 56 more rows #> Use `print(n = ...)` to see more rows #> 4 more variables: context, fmt_fn, warning, error #> reset_gtsummary_theme()