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 fromdata
. Summary statistics will be stratified by this variable. Default isNULL
.- 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 islist(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 viaassign_summary_digits()
. See below for details.- type
(
formula-list-selector
)
Specifies the summary type. Accepted value arec("continuous", "continuous2", "categorical", "dichotomous")
. If not specified, default type is assigned viaassign_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 isNULL
. See below for details.- sort
(
formula-list-selector
)
Specifies sorting to perform for categorical variables. Values must be one ofc("alphanumeric", "frequency")
. Default isall_categorical(FALSE) ~ "alphanumeric"
.- percent
(
string
)
Indicates the type of percentage to return. Must be one ofc("column", "row", "cell")
. Default is"column"
.- include
(
tidy-select
)
Variables to include in the summary table. Default iseverything()
.
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
# 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()