Skip to contents

Reorders the sections of a tbl_hierarchical() or tbl_hierarchical_count() table. "descending" (the default) orders the rows of each hierarchy variable by the sum of their counts across the table's columns, highest first, with ties keeping their order; "alphanumeric" restores the byte order of the level text. The overall row stays first, and each summary row stays above its section.

Usage

sort_hierarchical(x, sort = everything() ~ "descending", by_level = NULL, ...)

Arguments

x

(tbl_hierarchical, tbl_hierarchical_count)
A hierarchical table.

sort

(formula-list)
"descending" (the default) or "alphanumeric", keyed by the hierarchy variables; a single string applies to all of them.

by_level

(scalar)
One level of the table's by variable, e.g. by_level = "Placebo": "descending" then ranks the rows by the counts in that level's column alone. Default is NULL, the sum over the by levels. It has no effect on "alphanumeric", and errors when the table has no by variable.

...

Not used.

Value

The table with its rows reordered.

Details

Sorting a variable outside include descending has no stored counts to sum, so its sections are ordered by the summed rates of the first included variable below it, with a message: subjects counted once in each child rate can overlap, so the sum may exceed the section's true rate.

Examples

ae <- data.frame(
  id = c(1L, 1L, 1L, 2L, 2L, 3L, 5L, 5L, 6L),
  trt = c("A", "A", "A", "A", "A", "A", "B", "B", "B"),
  soc = c("Nervous", "Nervous", "Gastro", "Nervous", "Gastro",
          "Gastro", "Nervous", "Gastro", "Gastro"),
  term = c("Headache", "Headache", "Nausea", "Dizziness", "Nausea",
           "Vomiting", "Headache", "Nausea", "Nausea")
)
enrolled <- data.frame(id = 1:8, trt = rep(c("A", "B"), each = 4))

# Example 1 ----------------------------------
tbl_hierarchical(ae, variables = c(soc, term), by = trt,
                 denominator = enrolled, id = id) |>
  sort_hierarchical()
# Example 2 ---------------------------------- # order by the counts of one arm tbl_hierarchical(ae, variables = c(soc, term), by = trt, denominator = enrolled, id = id) |> sort_hierarchical(by_level = "B")