Demographic Table

Tingting Zhan

Introduction

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.

Note to Users

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.

Demographic Table

Data preparation

tgr = ToothGrowth |> 
  within.data.frame(expr = {
    dose = factor(dose) 
  })

Summary of all subjects

tgr |>
  DemographicTable(include = c('supp', 'len', 'dose')) |> 
  as_flextable()

Summary by one group

Color 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()

User may choose to hide the pp-values with option compare = FALSE.

tgr |>
  DemographicTable(groups = 'supp', include = c('len', 'dose'), compare = FALSE) |> 
  as_flextable()

Summary by multiple groups

tgr |>
  DemographicTable(groups = c('supp', 'dose'), include = c('len', 'supp')) |>
  as_flextable()

Contatenate multiple DemographicTables

tb1 = 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()

Exception Handling

Missing value in groups

MASS::survey |>
  DemographicTable(groups = c('M.I'), include = c('Pulse', 'Fold')) |>
  as_flextable()

Use of logical values

Using 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()

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()