Skip to contents

Formats as_characteristics_tibble() as a publication-ready table in the same APA style and through the same backends as as_attrition_table(): a bold heading row per variable, indented summary rows, one column per step headed by the step label and its n.

Usage

as_characteristics_table(
  flow,
  vars,
  backend = c("flextable", "gt", "huxtable"),
  steps = NULL,
  excluded = FALSE,
  level = NULL,
  numeric = "mean_sd",
  missing = c("ifany", "always", "never"),
  digits = 1L,
  drop_randomise = TRUE,
  assessed_label = "Assessed for eligibility",
  missing_label = "Missing",
  variable_col_label = "Characteristic",
  var_labels = NULL
)

Arguments

flow

A cf_flow object produced by apply_criteria().

vars

Character vector of column names to summarise.

backend

Character string: "flextable" (default), "gt", or "huxtable".

steps

Optional step selectors (a vector or list); see the section "Steps". NULL (default) means every step.

excluded

Logical. FALSE (default) describes the cohort surviving each step; TRUE describes the rows removed at each step.

level

Optional hierarchy level name at which to count units; see the section "Hierarchy levels". Requires a flow built with a hierarchy.

numeric

How to summarise numeric variables: "mean_sd" (default), "median_iqr", or both.

missing

When to include the row counting NA values per variable: "ifany" (default) if any column has one, "always", or "never".

digits

Decimal places for means, SDs, medians, quartiles and percentages. Default 1.

drop_randomise

Logical. Omit randomise() steps when steps is NULL (default TRUE).

assessed_label

Heading for step 0, the starting data. Default "Assessed for eligibility".

missing_label

Label of the row counting NA values. Default "Missing".

variable_col_label

Column header for the variable column. Default "Characteristic".

var_labels

Optional display names for the variables: a character vector named by column (e.g. c(age = "Age, years")), or an unnamed vector aligned with vars. Unlisted variables show their column name.

Value

A flextable, gt_tbl, or huxtable object.

Steps

By default there is one column per step from 0 (the starting data) to the last step, omitting randomise() steps (see drop_randomise). Column k holds cohort(flow, step_number = k): the rows that passed steps 1 to k. steps restricts or reorders the columns. Each element is resolved like at in cohort(): a number is a step number (0 = starting data); a string is a step label if one matches, else a category, meaning its last step. Steps named in steps are never dropped, even randomise() steps.

With excluded = TRUE, column k instead holds the rows removed at step k (those of excluded() with cf_step == k): the usual supplementary table of excluded participants' characteristics. Step 0 removes nothing and cannot be selected.

A column's heading is the step label, or assessed_label for step 0. Repeated headings are made unique with " #<n>".

Summaries

How a variable is summarised depends on its type:

  • numeric: a "Mean (SD)" row and/or a "Median [IQR]" row (shown as median [Q1, Q3]), chosen with numeric.

  • factor: one row per level of the factor, in level order, whether or not the level occurs; n (%) of the non-missing values.

  • character: as a factor whose levels are the distinct values in the starting data, sorted.

  • logical: a single row (level is NA) counting TRUE; n (%) of the non-missing values.

Each variable also gets a missing_label row counting NA values, as a percentage of the column's n; missing controls when it appears. Dates and other types are not summarised: convert them first.

Hierarchy levels

When the flow was built with apply_criteria(hierarchy = ), rows are summarised unless level names one of the hierarchy's levels, in which case each unit at that level (each distinct, complete key) contributes once and n counts units. A row with an NA in the level's key is not a unit. Every variable must then be constant within each unit across the starting data; a variable that varies within a unit is an error, since there is no single value to summarise.

Examples

# \donttest{
dat  <- mock_parallel_rct(n_participants = 200, seed = 1)
crit <- cf_criteria() |>
  include(~ age >= 18, label = "Adults only") |>
  include(~ eligible_screen, label = "Passed screening") |>
  exclude(~ withdrew, label = "Withdrew consent")
flow <- apply_criteria(dat, crit, id = "participant_id")

if (requireNamespace("flextable", quietly = TRUE) &&
    requireNamespace("officer", quietly = TRUE)) {
  as_characteristics_table(flow, vars = c("age", "sex", "withdrew"),
                           var_labels = c(age = "Age, years", sex = "Sex"))
}

Characteristic

Assessed for eligibility (n = 200)

Adults only (n = 184)

Passed screening (n = 169)

Withdrew consent (n = 162)

Age, years

Mean (SD)

44.6 (14.7)

45.7 (13.6)

45.2 (13.5)

45.5 (13.5)

Missing

10 (5.0%)

0 (0.0%)

0 (0.0%)

0 (0.0%)

Sex

F

97 (48.5%)

86 (46.7%)

80 (47.3%)

76 (46.9%)

M

98 (49.0%)

93 (50.5%)

85 (50.3%)

82 (50.6%)

O

5 (2.5%)

5 (2.7%)

4 (2.4%)

4 (2.5%)

withdrew

7 (3.5%)

7 (3.8%)

7 (4.1%)

0 (0.0%)

if (requireNamespace("gt", quietly = TRUE)) { as_characteristics_table(flow, vars = c("age", "sex"), backend = "gt", excluded = TRUE) }
Characteristic Adults only (n = 16) Passed screening (n = 15) Withdrew consent (n = 7)
age
Mean (SD) 11.2 (4.0) 51.6 (13.7) 37.4 (13.0)
Missing 10 (62.5%) 0 (0.0%) 0 (0.0%)
sex
F 11 (68.8%) 6 (40.0%) 4 (57.1%)
M 5 (31.2%) 8 (53.3%) 3 (42.9%)
O 0 (0.0%) 1 (6.7%) 0 (0.0%)
# }