This function is used to sort stacked hierarchical ARDs.
For the purposes of this function, we define a "variable group" as a combination of ARD rows grouped by the
combination of all their variable levels, but excluding any by variables.
sort_ard_hierarchical(x, sort = everything() ~ "descending", by_level = NULL)(card)
a stacked hierarchical ARD of class 'card' created using ard_stack_hierarchical() or
ard_stack_hierarchical_count().
(formula-list-selector, string)
a named list, a list of formulas, a single formula where the list element is a named list of functions
(or the RHS of a formula), or a single string specifying the types of sorting to perform at each hierarchy variable
level. If the sort method for any variable is not specified then the method will default to "descending". If a
single unnamed string is supplied it is applied to all variables. For each variable, the value specified must
be one of:
"alphanumeric" - at the specified hierarchy level of the ARD, groups are ordered alphanumerically
(i.e. A to Z) by variable_level text.
"descending" - within each variable group of the ARD at the specified hierarchy level, count sums are
calculated for each group and groups are sorted in descending order by sum. When sort is "descending" for a
given variable and n is included in statistic for the variable then n is used to calculate variable group
sums, otherwise p is used. If neither n nor p are present in x for the variable, an error will occur.
Defaults to everything() ~ "descending".
(named list)
a named list used to restrict the counts used for "descending" sorting to one or more specific by variable
levels. Each name must be one of the by variables used to create x, and each element must be a single level of
that by variable. When supplied, "descending" sorts rank variable groups by the count sums calculated only from
the rows matching every specified by variable level (e.g. list(TRTA = "Placebo") sorts by the counts observed
in the "Placebo" arm), rather than the sums across all by variable levels. Any by variable not named in the
list is not restricted. This argument has no effect on variables sorted "alphanumeric".
Defaults to NULL, in which case count sums are calculated across all by variable levels.
an ARD data frame of class 'card'
If overall data is present in x (i.e. the ARD was created with ard_stack_hierarchical(overall=TRUE)), the
overall data will be sorted last within each variable group (i.e. after any other rows with the same combination of
variable levels).
ard_stack_hierarchical(
ADAE,
variables = c(AESOC, AEDECOD),
by = TRTA,
denominator = ADSL,
id = USUBJID
) |>
sort_ard_hierarchical(AESOC ~ "alphanumeric")
#> # An ARD data frame: 2,394 × 13
#> group1 group1_level group2 group2_level variable variable_level stat_name
#> <chr> <list> <chr> <list> <chr> <list> <chr>
#> 1 NA <NULL> NA <NULL> TRTA Placebo n
#> 2 NA <NULL> NA <NULL> TRTA Placebo N
#> 3 NA <NULL> NA <NULL> TRTA Placebo p
#> 4 NA <NULL> NA <NULL> TRTA Xanomeline High D… n
#> 5 NA <NULL> NA <NULL> TRTA Xanomeline High D… N
#> 6 NA <NULL> NA <NULL> TRTA Xanomeline High D… p
#> 7 NA <NULL> NA <NULL> TRTA Xanomeline Low Do… n
#> 8 NA <NULL> NA <NULL> TRTA Xanomeline Low Do… N
#> 9 NA <NULL> NA <NULL> TRTA Xanomeline Low Do… p
#> 10 TRTA Placebo NA <NULL> AESOC CARDIAC DISORDERS n
#> # ℹ 2,384 more rows
#> # ℹ 6 more variables: context <chr>, stat_label <chr>, stat <list>,
#> # fmt_fun <list>, warning <list>, error <list>
ard_stack_hierarchical_count(
ADAE,
variables = c(AESOC, AEDECOD),
by = TRTA,
denominator = ADSL
) |>
sort_ard_hierarchical(sort = list(AESOC ~ "alphanumeric", AEDECOD ~ "descending"))
#> # An ARD data frame: 804 × 13
#> group1 group1_level group2 group2_level variable variable_level stat_name
#> <chr> <list> <chr> <list> <chr> <list> <chr>
#> 1 NA <NULL> NA <NULL> TRTA Placebo n
#> 2 NA <NULL> NA <NULL> TRTA Placebo N
#> 3 NA <NULL> NA <NULL> TRTA Placebo p
#> 4 NA <NULL> NA <NULL> TRTA Xanomeline High D… n
#> 5 NA <NULL> NA <NULL> TRTA Xanomeline High D… N
#> 6 NA <NULL> NA <NULL> TRTA Xanomeline High D… p
#> 7 NA <NULL> NA <NULL> TRTA Xanomeline Low Do… n
#> 8 NA <NULL> NA <NULL> TRTA Xanomeline Low Do… N
#> 9 NA <NULL> NA <NULL> TRTA Xanomeline Low Do… p
#> 10 TRTA Placebo NA <NULL> AESOC CARDIAC DISORDERS n
#> # ℹ 794 more rows
#> # ℹ 6 more variables: context <chr>, stat_label <chr>, stat <list>,
#> # fmt_fun <list>, warning <list>, error <list>
# sort by the counts observed in the "Placebo" arm only
ard_stack_hierarchical(
ADAE,
variables = c(AESOC, AEDECOD),
by = TRTA,
denominator = ADSL,
id = USUBJID
) |>
sort_ard_hierarchical(by_level = list(TRTA = "Placebo"))
#> # An ARD data frame: 2,394 × 13
#> group1 group1_level group2 group2_level variable variable_level stat_name
#> <chr> <list> <chr> <list> <chr> <list> <chr>
#> 1 NA <NULL> NA <NULL> TRTA Placebo n
#> 2 NA <NULL> NA <NULL> TRTA Placebo N
#> 3 NA <NULL> NA <NULL> TRTA Placebo p
#> 4 NA <NULL> NA <NULL> TRTA Xanomeline High D… n
#> 5 NA <NULL> NA <NULL> TRTA Xanomeline High D… N
#> 6 NA <NULL> NA <NULL> TRTA Xanomeline High D… p
#> 7 NA <NULL> NA <NULL> TRTA Xanomeline Low Do… n
#> 8 NA <NULL> NA <NULL> TRTA Xanomeline Low Do… N
#> 9 NA <NULL> NA <NULL> TRTA Xanomeline Low Do… p
#> 10 TRTA Placebo NA <NULL> AESOC GENERAL DISORDERS… n
#> # ℹ 2,384 more rows
#> # ℹ 6 more variables: context <chr>, stat_label <chr>, stat <list>,
#> # fmt_fun <list>, warning <list>, error <list>