Cluster-wise summary table using a separate cluster vector (gtsummary + gt)
Source:R/summarizers.R
cluster_summary.RdProduce a cluster-stratified summary table using gtsummary, where the
cluster assignments are supplied as a separate vector.
All additional arguments (...) are passed directly to
gtsummary::tbl_summary(), so users can specify
all_continuous() / all_categorical() selectors and custom statistics.
Arguments
- data
A data.frame or tibble of features to summarize.
- clusters
A vector (factor, character, or numeric) of cluster labels with length equal to
nrow(data).- add_options
List of post-processing options:
add_overall(defaultFALSE): add overall columnadd_n(defaultTRUE) : add group Nsadd_p(defaultFALSE): add p-values
- return_as
"gtsummary"(default) or"gt". When"gt", the function callsgtsummary::as_gt()for rendering.- include
Optional character vector of variables to include. Defaults to all columns in
data.- ...
Passed to
gtsummary::tbl_summary()(e.g.,statistic=,type=,digits=,missing=,label=, etc.).
Value
A gtsummary::tbl_summary (default) or gt::gt_tbl if return_as="gt".
Examples
if(requireNamespace("gtsummary")){
df <- data.frame(
age = rnorm(100, 60, 10),
bmi = rnorm(100, 28, 5),
sex = sample(c("F","M"), 100, TRUE)
)
cl <- sample(1:3, 100, TRUE)
cluster_summary(
data = df,
clusters = cl,
statistic = list(
gtsummary::all_continuous() ~ "{mean} ({sd})",
gtsummary::all_categorical() ~ "{n} / {N} ({p}%)"
),
missing = "always"
)
}
Characteristic
N
1
N = 311
2
N = 341
3
N = 351
1 Mean (SD); n / N (%)