This function is used to filter hierarchical table rows. Filters are not applied to summary or overall rows.
Arguments
- x
(
tbl_hierarchical
,tbl_hierarchical_count
)
A hierarchical gtsummary table of class'tbl_hierarchical'
or'tbl_hierarchical_count'
.- filter
(
expression
)
An expression that is used to filter rows of the table. See the Details section below.- keep_empty
(scalar
logical
)
Logical argument indicating whether to retain summary rows corresponding to table hierarchy sections that have had all rows filtered out. Default isFALSE
.
Details
The filter
argument can be used to filter out rows of a table which do not meet the criteria provided as an
expression. Rows can be filtered on the values of any of the possible statistics (n
, p
, and N
) provided they
are included at least once in the table, as well as the values of any by
variables. Filtering is only applied to
rows that correspond to the innermost variable in the hierarchy - all outer variable (summary) rows are kept
regardless of whether they meet the filtering criteria themselves. In addition to filtering on individual statistic
values, filters can be applied across the row (i.e. across all by
variable values) by using aggregate functions
such as sum()
and mean()
.
If an overall column was added to the table (via add_overall())
) this column will not be used in any filters (i.e.
sum(n)
will not include the overall n
in a given row). To filter on overall statistics use the sum()
function
in your filter instead (i.e. sum(n)
is equal to the overall column n
across any by
variables).
Some examples of possible filters:
filter = n > 5
: keep rows where one of the treatment groups observed more than 5 AEsfilter = n == 2 & p < 0.05
: keep rows where one of the treatment groups observed exactly 2 AEs and one of the treatment groups observed a proportion less than 5%.filter = sum(n) >= 4
: keep rows where there were 4 or more AEs observed across the rowfilter = mean(n) > 4 | n > 3
: keep rows where the mean number of AEs is 4 or more across the row or one of the treatment groups observed more than 3 AEsfilter = any(n > 2 & TRTA == "Xanomeline High Dose")
: keep rows where the"Xanomeline High Dose"
treatment group observed more than 2 AEs
Examples
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
)
# Example 1 ----------------------------------
# Keep rows where less than 2 AEs are observed across the row
filter_hierarchical(tbl, sum(n) < 2)
Body System or Organ Class
Dictionary-Derived Term
Placebo
N = 861
Xanomeline High Dose
N = 841
Xanomeline Low Dose
N = 841
1 n (%)
# Example 2 ----------------------------------
# Keep rows where at least one treatment group in the row has at least 2 AEs observed
filter_hierarchical(tbl, n >= 2)
Body System or Organ Class
Dictionary-Derived Term
Placebo
N = 861
Xanomeline High Dose
N = 841
Xanomeline Low Dose
N = 841
1 n (%)
# Example 3 ----------------------------------
# Keep rows where AEs across the row have an overall prevalence of greater than 0.5%
filter_hierarchical(tbl, sum(n) / sum(N) > 0.005)
Body System or Organ Class
Dictionary-Derived Term
Placebo
N = 861
Xanomeline High Dose
N = 841
Xanomeline Low Dose
N = 841
1 n (%)