library(DemographicTable)
library(flextable)
set_flextable_defaults(font.size = 9)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.
Examples in this vignette require that the search path has
library(DemographicTable)
library(flextable)
set_flextable_defaults(font.size = 9)Users may remove the last pipe |> as_flextable() from all examples. This is required in the vignette to make quarto rendering work.
Data preparation
tgr = ToothGrowth |>
within.data.frame(expr = {
dose = factor(dose)
})tgr |>
DemographicTable(include = c('supp', 'len', 'dose')) |>
as_flextable()
| tgr |
|---|---|
n=60 | |
len. |
|
dose: n (%). |
|
supp: n (%). |
|
| n=60 |
tgr |
groupColor of each individual group is determined by scales::pal_hue(), which is the default color pallete used in package ggplot2.
tgr |>
DemographicTable(groups = 'supp', include = c('len', 'dose')) |>
as_flextable()
| tgr | |||
|---|---|---|---|---|
n=60 | supp | |||
OJ | VC | Signif | ||
len. |
|
|
| 0.064 |
dose: n (%). |
|
|
| 1.000 |
| n=60 | supp | ||
tgr | ||||
User may choose to hide the p-values with option compare = FALSE.
tgr |>
DemographicTable(groups = 'supp', include = c('len', 'dose'), compare = FALSE) |>
as_flextable()
| tgr | ||
|---|---|---|---|
n=60 | supp | ||
OJ | VC | ||
len. |
|
|
|
dose: n (%). |
|
|
|
| n=60 | supp | |
tgr | |||
groupstgr |>
DemographicTable(groups = c('supp', 'dose'), include = c('len', 'supp')) |>
as_flextable()
| tgr | |||||||
|---|---|---|---|---|---|---|---|---|
n=60 | supp | dose | ||||||
OJ | VC | Signif1 | 0.5 | 1 | 2 | Signif2 | ||
len. |
|
|
| 0.064 |
|
|
| ★ 0.000; ⸢1⸥ vs. ⸢0.5⸥ |
supp: n (%). |
|
|
| ★ 0.000 |
|
|
| 1.000 |
| n=60 | supp | dose | |||||
tgr | ||||||||
DemographicTablestb1 = CO2 |>
DemographicTable(groups = 'Type', include = c('conc', 'uptake'))tb2 = CO2 |>
subset(subset = (Treatment == 'nonchilled')) |>
DemographicTable(groups = 'Type', include = c('conc', 'uptake'), data.name = 'CO2_nonchilled')c(tb1, tb2) |> as_flextable()
| CO2 | CO2_nonchilled | ||||||
|---|---|---|---|---|---|---|---|---|
n=84 | Type | n=42 | Type | |||||
Quebec | Mississippi | Signif1 | Quebec | Mississippi | Signif2 | |||
conc. |
|
|
| 1.000 |
|
|
| 1.000 |
uptake. |
|
|
| 0.000★ |
|
|
| 0.000★ |
| n=84 | Type | n=42 | Type | ||||
CO2 | CO2_nonchilled | |||||||
groupsMASS::survey |>
DemographicTable(groups = c('M.I'), include = c('Pulse', 'Fold')) |>
as_flextable()
| MASS::survey | |||
|---|---|---|---|---|
n=237 | M.I | |||
Imperial | Metric | Signif | ||
Pulse. | n*=192 | n*=59 | n*=112 | 0.974 |
Fold: n (%). |
|
|
| 0.532 |
| n=237 | M.I | ||
MASS::survey | ||||
logical valuesUsing logical values is discouraged, as this practice is proved confusing to scientists without a strong data background.
mtc = mtcars |>
within.data.frame(expr = {
vs_straight = as.logical(vs)
am_manual = as.logical(am)
})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() |>
suppressWarnings()
| mtc | |||
|---|---|---|---|---|
n=32 | am_manual | |||
FALSE | TRUE | Signif | ||
drat. |
|
|
| 0.000★ |
vs_straight: n (%) | 14 (43.8%) | 7 (36.8%) | 7 (53.8%) | 0.556 |
| n=32 | am_manual | ||
mtc | ||||
We recommend using 2-level factors.
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()
| mtcars | |||
|---|---|---|---|---|
n=32 | am | |||
automatic | manual | Signif | ||
drat. |
|
|
| 0.000★ |
vs: n (%). |
|
|
| 0.473 |
| n=32 | am | ||
mtcars | ||||