Skip to contents

[Experimental]

This function is used to sort hierarchical tables. Options for sorting criteria are:

  1. Descending - within each section of the hierarchy table, event rate sums are calculated for each row and rows are sorted in descending order by sum (default).

  2. Alphanumeric - rows are ordered alphanumerically (i.e. A to Z) by label text. By default, tbl_hierarchical() sorts tables in alphanumeric order.

Usage

sort_hierarchical(x, sort = c("descending", "alphanumeric"))

Arguments

x

(tbl_hierarchical, tbl_hierarchical_count)
a hierarchical gtsummary table of class 'tbl_hierarchical' or 'tbl_hierarchical_count'.

sort

(string)
type of sorting to perform. Value must be one of:

  • "alphanumeric" - at each hierarchy level of the table, rows are ordered alphanumerically (i.e. A to Z) by label text.

  • "descending" - at each hierarchy level of the table, count sums are calculated for each row and rows are sorted in descending order by sum. If sort = "descending", the n statistic is used to calculate row sums if included in statistic for all variables, otherwise p is used. If neither n nor p are present in x for all variables, an error will occur.

Defaults to "descending".

Value

A gtsummary of the same class as x.

Examples

theme_gtsummary_compact()
#> Setting theme "Compact"
ADAE_subset <- cards::ADAE |>
  dplyr::filter(AEBODSYS %in% c("SKIN AND SUBCUTANEOUS TISSUE DISORDERS",
                                "EAR AND LABYRINTH DISORDERS")) |>
  dplyr::filter(.by = AEBODSYS, dplyr::row_number() < 20)

tbl <-
  tbl_hierarchical(
    data = ADAE_subset,
    variables = c(AEBODSYS, AEDECOD),
    by = TRTA,
    denominator = cards::ADSL |> mutate(TRTA = ARM),
    id = USUBJID,
    overall_row = TRUE
  ) |>
  add_overall()


# Example 1 - Descending Frequency Sort ------------------
sort_hierarchical(tbl)
Body System or Organ Class
    Dictionary-Derived Term
Overall
N = 254
1
Placebo
N = 86
1
Xanomeline High Dose
N = 84
1
Xanomeline Low Dose
N = 84
1
Number of patients with event 11 (4.3%) 3 (3.5%) 3 (3.6%) 5 (6.0%)
SKIN AND SUBCUTANEOUS TISSUE DISORDERS 7 (2.8%) 2 (2.3%) 2 (2.4%) 3 (3.6%)
    ERYTHEMA 4 (1.6%) 1 (1.2%) 0 (0%) 3 (3.6%)
    PRURITUS 3 (1.2%) 1 (1.2%) 0 (0%) 2 (2.4%)
    ACTINIC KERATOSIS 1 (0.4%) 0 (0%) 1 (1.2%) 0 (0%)
    PRURITUS GENERALISED 1 (0.4%) 0 (0%) 0 (0%) 1 (1.2%)
    RASH 1 (0.4%) 0 (0%) 1 (1.2%) 0 (0%)
EAR AND LABYRINTH DISORDERS 4 (1.6%) 1 (1.2%) 1 (1.2%) 2 (2.4%)
    VERTIGO 2 (0.8%) 0 (0%) 1 (1.2%) 1 (1.2%)
    CERUMEN IMPACTION 1 (0.4%) 0 (0%) 0 (0%) 1 (1.2%)
    EAR PAIN 1 (0.4%) 1 (1.2%) 0 (0%) 0 (0%)
    TINNITUS 1 (0.4%) 0 (0%) 0 (0%) 1 (1.2%)
1 n (%)
# Example 2 - Alphanumeric Sort -------------------------- sort_hierarchical(tbl, sort = "alphanumeric")
Body System or Organ Class
    Dictionary-Derived Term
Overall
N = 254
1
Placebo
N = 86
1
Xanomeline High Dose
N = 84
1
Xanomeline Low Dose
N = 84
1
Number of patients with event 11 (4.3%) 3 (3.5%) 3 (3.6%) 5 (6.0%)
EAR AND LABYRINTH DISORDERS 4 (1.6%) 1 (1.2%) 1 (1.2%) 2 (2.4%)
    CERUMEN IMPACTION 1 (0.4%) 0 (0%) 0 (0%) 1 (1.2%)
    EAR PAIN 1 (0.4%) 1 (1.2%) 0 (0%) 0 (0%)
    TINNITUS 1 (0.4%) 0 (0%) 0 (0%) 1 (1.2%)
    VERTIGO 2 (0.8%) 0 (0%) 1 (1.2%) 1 (1.2%)
SKIN AND SUBCUTANEOUS TISSUE DISORDERS 7 (2.8%) 2 (2.3%) 2 (2.4%) 3 (3.6%)
    ACTINIC KERATOSIS 1 (0.4%) 0 (0%) 1 (1.2%) 0 (0%)
    ERYTHEMA 4 (1.6%) 1 (1.2%) 0 (0%) 3 (3.6%)
    PRURITUS 3 (1.2%) 1 (1.2%) 0 (0%) 2 (2.4%)
    PRURITUS GENERALISED 1 (0.4%) 0 (0%) 0 (0%) 1 (1.2%)
    RASH 1 (0.4%) 0 (0%) 1 (1.2%) 0 (0%)
1 n (%)
reset_gtsummary_theme()