Flattens the Analysis Results Data (ARD) of a table into a single ARD.

Stacked and stratified tables (e.g. from gtsummary::tbl_strata() or gtsummary::tbl_strata_nested_stack()) return one ARD per stratum from gtsummary::gather_ard(), with the stratum recorded only in the list names. simplify_ard() adds each stratum as a group column, numbered as cards::ard_strata() does: the groups of the table keep their numbers and the strata take the numbers after the highest one, in the order of strata. The strata of a nested call come before those of the call around it, as with nested cards::ard_strata() calls. A stratum lands in the same column everywhere in the table.

The strata levels of gtsummary::tbl_strata() tables keep their type (e.g. factor or numeric). gtsummary::tbl_strata_nested_stack() tables and lists of ARDs keep the levels only as text, so there they are character.

Stacked tables without strata whose pieces report the same statistics (e.g. gtsummary::tbl_stack() of the same variables on two subsets) are told apart by a tbl_id1 group (tbl_id2 for the next level of stacking), with the tbl_ids of the stack, or the position of the piece, as level.

Split tables (gtsummary::tbl_split_by_rows(), gtsummary::tbl_split_by_columns()) carry a full copy of the ARD in every piece. These copies are removed.

The result can be compared with an independently derived ARD using cards::compare_ard().

simplify_ard(x, .deduplicate = TRUE)

Arguments

x

(gtsummary, tbl_split or list)
a table, a list of split tables, or a (nested) list of ARDs as returned by gtsummary::gather_ard().

.deduplicate

(scalar logical)
whether to remove duplicated statistics. Repeated copies of a whole ARD (e.g. from split tables) are removed with a message. Rows shared by several ARDs (e.g. variable attributes repeated by add_overall()) are removed silently. Default is TRUE.

Value

A card_simplified object (a card). Calling simplify_ard() on its own output returns it unchanged. The same statistic (same groups, variables, context and stat_name) with different values in two places of the table is an error.

Details

Statistics are identified by their groups, variables, context and stat_name. context is needed for tables that report two statistics with the same name for the same rows, e.g. subject and event counts in tbl_hierarchical_rate_and_count(). Pass the same keys to cards::compare_ard(), as column names (cards 0.9.0 does not accept a selection of columns there). With ard_tbl and ard_qc from the last example:

keys <- names(dplyr::select(
  ard_qc,
  cards::all_ard_groups(), cards::all_ard_variables(), "context", "stat_name"
))
cards::compare_ard(ard_tbl, ard_qc, keys = keys, columns = c("stat_label", "stat"))

Tools that need atomic columns (e.g. diffdf::diffdf()) compare the statistics: filter them first, then use cards::unlist_ard_columns(). The "attributes" rows hold text and sometimes several values (e.g. the levels of a factor), so with them stat stays a list or turns into character.

Examples

adlb <- cards::ADLB |>
  dplyr::mutate(AVISIT = trimws(AVISIT)) |>
  dplyr::filter(
    PARAMCD %in% c("SODIUM", "K"),
    AVISIT %in% c("Baseline", "Week 2", "Week 4")
  )

# one table per parameter, split into one page per parameter
tbl <- gtsummary::tbl_strata(
  adlb,
  strata = PARAM,
  .tbl_fun = ~ tbl_baseline_chg(
    .x,
    baseline_level = "Baseline",
    by = "TRTA",
    denominator = cards::ADSL
  ),
  .combine_with = "tbl_stack",
  .combine_args = list(group_header = NULL, quiet = TRUE)
) |>
  gtsummary::tbl_split_by_rows(variable_level = ends_with("lbl"))
#> ℹ Converting column "TRTA" to a factor.
#> ℹ Converting column "TRTA" to a factor.

# PARAM becomes group2, next to the treatment in group1
simplify_ard(tbl)
#> Removed 2 repeated copies of an ARD.
#> ℹ Split tables, e.g. from `gtsummary::tbl_split_by_rows()`, carry a full copy
#>   of the ARD in every piece.
#> # An ARD data frame: 344 × 14
#>    group1 group1_level group2 group2_level     variable variable_level stat_name
#>    <chr>  <list>       <chr>  <list>           <chr>    <list>         <chr>    
#>  1 TRTA   Placebo      PARAM  Potassium (mmol… AVAL     Baseline       mean     
#>  2 TRTA   Placebo      PARAM  Potassium (mmol… AVAL     Baseline       sd       
#>  3 TRTA   Placebo      PARAM  Potassium (mmol… AVAL     Baseline       median   
#>  4 TRTA   Placebo      PARAM  Potassium (mmol… AVAL     Baseline       min      
#>  5 TRTA   Placebo      PARAM  Potassium (mmol… AVAL     Baseline       max      
#>  6 TRTA   Placebo      PARAM  Potassium (mmol… AVAL     Week 2         mean     
#>  7 TRTA   Placebo      PARAM  Potassium (mmol… AVAL     Week 2         sd       
#>  8 TRTA   Placebo      PARAM  Potassium (mmol… AVAL     Week 2         median   
#>  9 TRTA   Placebo      PARAM  Potassium (mmol… AVAL     Week 2         min      
#> 10 TRTA   Placebo      PARAM  Potassium (mmol… AVAL     Week 2         max      
#> # ℹ 334 more rows
#> # ℹ 7 more variables: context <chr>, stat_label <chr>, stat <list>,
#> #   fmt_fun <list>, warning <list>, error <list>, gts_column <chr>

# atomic columns, e.g. for diffdf: keep the statistics, then unlist
simplify_ard(tbl) |>
  dplyr::filter(context == "summary") |>
  cards::unlist_ard_columns()
#> Removed 2 repeated copies of an ARD.
#> ℹ Split tables, e.g. from `gtsummary::tbl_split_by_rows()`, carry a full copy
#>   of the ARD in every piece.
#> # A tibble: 150 × 14
#>    group1 group1_level group2 group2_level       variable variable_level context
#>    <chr>  <chr>        <chr>  <chr>              <chr>    <chr>          <chr>  
#>  1 TRTA   Placebo      PARAM  Potassium (mmol/L) AVAL     Baseline       summary
#>  2 TRTA   Placebo      PARAM  Potassium (mmol/L) AVAL     Baseline       summary
#>  3 TRTA   Placebo      PARAM  Potassium (mmol/L) AVAL     Baseline       summary
#>  4 TRTA   Placebo      PARAM  Potassium (mmol/L) AVAL     Baseline       summary
#>  5 TRTA   Placebo      PARAM  Potassium (mmol/L) AVAL     Baseline       summary
#>  6 TRTA   Placebo      PARAM  Potassium (mmol/L) AVAL     Week 2         summary
#>  7 TRTA   Placebo      PARAM  Potassium (mmol/L) AVAL     Week 2         summary
#>  8 TRTA   Placebo      PARAM  Potassium (mmol/L) AVAL     Week 2         summary
#>  9 TRTA   Placebo      PARAM  Potassium (mmol/L) AVAL     Week 2         summary
#> 10 TRTA   Placebo      PARAM  Potassium (mmol/L) AVAL     Week 2         summary
#> # ℹ 140 more rows
#> # ℹ 7 more variables: stat_name <chr>, stat_label <chr>, stat <dbl>,
#> #   fmt_fun <list>, warning <list>, error <list>, gts_column <chr>

# the same statistics built with cards only, e.g. by an independent QC program
adlb_wk2 <- dplyr::filter(adlb, AVISIT == "Week 2")
ard_tbl <- gtsummary::tbl_strata(
  adlb_wk2,
  strata = PARAM,
  .tbl_fun = ~ tbl_roche_summary(.x, by = TRTA, include = AVAL, type = AVAL ~ "continuous2"),
  .combine_with = "tbl_stack",
  .combine_args = list(group_header = NULL, quiet = TRUE)
) |>
  simplify_ard() |>
  dplyr::filter(context == "summary")

ard_qc <- cards::ard_strata(
  adlb_wk2,
  .strata = PARAM,
  .f = \(df) {
    cards::ard_summary(
      df,
      by = TRTA,
      variables = AVAL,
      statistic = ~ cards::continuous_summary_fns(c("mean", "sd", "median", "min", "max"))
    )
  }
)

keys <- names(dplyr::select(
  ard_qc,
  cards::all_ard_groups(), cards::all_ard_variables(), "context", "stat_name"
))
cards::compare_ard(ard_tbl, ard_qc, keys = keys, columns = c("stat_label", "stat")) |>
  cards::is_ard_equal()
#> [1] TRUE