Descriptive summary table with group comparisons (Table 1)
Source:R/tbl_descriptive.R
tbl_descriptive.RdProduces a descriptive/Table 1-style summary of a data frame, with optional group comparisons. Continuous variables are summarised as mean ± SD and compared with an independent-samples t-test (two groups) or one-way ANOVA (three or more groups). Categorical variables are summarised as n (%) and compared with a chi-squared test.
Arguments
- data
A data frame.
- group
<
tidy-select> Unquoted name of the grouping variable. IfNULL(default) the overall sample is summarised without a comparison column.- vars
<
tidy-select> Variables to include. Defaults to all columns exceptgroup.- domains
A named list mapping variable names to domain/section labels, e.g.
list("Metabolic" = c("hdl", "glucose"), "Anthropometric" = c("bmi", "waist")). Variables not mentioned are placed in an "Other" section.- log_vars
Character vector of variable names that were log-transformed prior to analysis. A footnote is appended noting that values are shown on the raw scale.
- digits
Integer. Number of decimal places for continuous variables (default
2).- p_digits
Integer. Number of decimal places for p-values (default
3).- fdr
Logical. Apply BH FDR correction to p-values across all tests (default
FALSE).- overall
Logical. Include an overall (ungrouped) column alongside group columns (default
TRUE).- output
Character string specifying the render target. One of
"gt"(default, for Word/PDF viagt),"html"(interactivereactable), or"latex"(LaTeX viagt::as_latex()). This value is stored on the returned object and used byclerk_render()to dispatch to the correct renderer automatically.
Value
A clerk_tbl object (a list with class "clerk_tbl") containing:
tableA data frame with one row per variable.
domainsThe domain list supplied by the user.
log_varsThe log-transformed variable names.
typeCharacter string
"descriptive".groupName of the grouping variable, or
NULL.outputThe render target:
"gt","html", or"latex".
Examples
tbl_descriptive(
clerk_example,
group = sex,
domains = list(
"Metabolic" = c("hdl", "glucose", "bmi"),
"Cognitive" = c("tmt_time", "verbal_fluency"),
"Mental health"= c("bdi", "panas_neg")
),
log_vars = "tmt_time",
output = "gt"
) |> clerk_render(title = "Table 1. Sample characteristics by sex")
#> Warning: invalid factor level, NA generated