View source: R/sort_hierarchical.R
| sort_hierarchical | R Documentation |
This function is used to sort hierarchical tables. Options for sorting criteria are:
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).
Alphanumeric - rows are ordered alphanumerically (i.e. A to Z) by label text. By default, tbl_hierarchical()
sorts tables in alphanumeric order.
sort_hierarchical(x, ...)
## S3 method for class 'tbl_hierarchical'
sort_hierarchical(x, sort = everything() ~ "descending", by_level = NULL, ...)
## S3 method for class 'tbl_hierarchical_count'
sort_hierarchical(x, sort = everything() ~ "descending", by_level = NULL, ...)
## S3 method for class 'tbl_ard_hierarchical'
sort_hierarchical(x, sort = everything() ~ "descending", by_level = NULL, ...)
x |
( |
... |
These dots are for future extensions and must be empty. |
sort |
(
Defaults to |
by_level |
(scalar) Defaults to |
If you do not want to display rates for a hierarchy variable from table x but you would like to sort by descending
frequency for that variable, the recommended method is to keep the variable in include when calling
tbl_hierarchical() so that rates for this variable are available, sort the table as needed using
sort_hierarchical(), and then finally remove these rates from the table using modify_table_body().
For example, to remove rates from the AESOC rows in hierarchy table x, you can call:
x |>
modify_table_body(
\(df) mutate(df, dplyr::across(all_stat_cols(), ~ifelse(variable %in% "AESOC", NA, .)))
)
a gtsummary table of the same class as x.
When sorting a table that includes an overall column add_overall() must be called to add the overall column
before sort_hierarchical() is called.
filter_hierarchical()
theme_gtsummary_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,
id = USUBJID,
overall_row = TRUE
) |>
add_overall()
# Example 1 ----------------------------------------------
# Sort all variables by descending frequency (default)
sort_hierarchical(tbl)
# Example 2 ----------------------------------------------
# Sort all variables alphanumerically
sort_hierarchical(tbl, sort = everything() ~ "alphanumeric")
# Example 3 ----------------------------------------------
# Sort `AEBODSYS` alphanumerically, `AEDECOD` by descending frequency
sort_hierarchical(tbl, sort = list(AEBODSYS = "alphanumeric", AEDECOD = "descending"))
# Example 4 ----------------------------------------------
# Sort by descending frequency within the "Placebo" arm only
sort_hierarchical(tbl, by_level = "Placebo")
reset_gtsummary_theme()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.