This vignette of package DemographicTable (CRAN, Github) presents an idiot-proof interface to create a summary table of simple statistics, often known as a demographic table.
Package DemographicTableImports packages
cli(Csárdi 2025, v3.6.5), for attractive command line interfaces
🗝 flextable(Gohel and Skintzos 2025, v0.9.9) and officer(Gohel, Moog, and Heckmann 2025, v0.6.10), key dependencies, for flexible table
scales(Wickham, Pedersen, and Seidel 2025, v1.4.0), for colour pallete
Prerequisite
Packages DemographicTable requires R version 4.5.0 (released 2025-04-11) or higher (macOS, Windows). An Integrated Development Environment (IDE), e.g., RStudio or Positron, is not required, but highly recommended. This vignette is created under R version 4.5.1 (2025-06-13) using packages knitr(Xie 2025, v1.50), quarto(Allaire and Dervieux 2025, v1.5.0 with Quarto v1.7.33) and rmarkdown(Allaire et al. 2024, v2.29).
Environment on author’s computer
Sys.info()[c('sysname', 'release', 'machine')]# sysname release machine # "Darwin" "24.6.0" "arm64"R.version# _ # platform aarch64-apple-darwin20 # arch aarch64 # os darwin20 # system aarch64, darwin20 # status # major 4 # minor 5.1 # year 2025 # month 06 # day 13 # svn rev 88306 # language R # version.string R version 4.5.1 (2025-06-13)# nickname Great Square Root
Experimental (and maybe unstable) features are released extremely frequently to Github. Stable releases to CRAN are typically updated every 2 to 3 months.
A warning message will be printed if logical variables are used in groups and/or include.
tryCatch(DemographicTable(mtc, groups ='am_manual', include =c('drat', 'vs_straight')), warning = identity)# <simpleWarning in DemographicTable(mtc, groups = "am_manual", include = c("drat", "vs_straight")): # Some scientists do not understand logical value (e.g., arm_intervention being TRUE/FALSE)# Consider using 2-level factor (e.g., arm being intervention/control)>
Instead of using logical variables
mtc |>DemographicTable(groups ='am_manual', include =c('drat', 'vs_straight')) |>as_flextable()
mtc
n=32
am_manual
FALSE n=19 (59.4%)
TRUE n=13 (40.6%)
Signif
drat. mean±sd median; IQR range
3.6±0.5 . 2.8~4.9
3.3±0.4 3.1; 0.6 2.8~3.9
4.0±0.4 . 3.5~4.9
0.000★ Wilcoxon- Mann-Whitney
vs_straight: n (%)
14 (43.8%)
7 (36.8%)
7 (53.8%)
0.556 χ² (chi-square)
n=32
am_manual
mtc
We recommend using 2-levelfactors.
mtcars |>within.data.frame(expr = { vs =ifelse(vs, yes ='Straight', no ='V-shaped') am =ifelse(am, yes ='manual', no ='automatic') }) |>DemographicTable(groups ='am', include =c('drat', 'vs'), data.name ='mtcars') |>as_flextable()
Allaire, JJ, Yihui Xie, Christophe Dervieux, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, et al. 2024. rmarkdown: Dynamic Documents for r. https://github.com/rstudio/rmarkdown.