View source: R/filter_hierarchical.R
filter_hierarchical | R Documentation |
This function is used to filter hierarchical table rows. Filters are not applied to summary or overall rows.
filter_hierarchical(x, ...)
## S3 method for class 'tbl_hierarchical'
filter_hierarchical(x, filter, keep_empty = FALSE, ...)
## S3 method for class 'tbl_hierarchical_count'
filter_hierarchical(x, filter, keep_empty = FALSE, ...)
x |
( |
... |
These dots are for future extensions and must be empty. |
filter |
( |
keep_empty |
(scalar |
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 AEs
filter = 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 row
filter = 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 AEs
filter = any(n > 2 & TRTA == "Xanomeline High Dose")
: keep rows where the "Xanomeline High Dose"
treatment
group observed more than 2 AEs
A gtsummary
of the same class as x
.
sort_hierarchical()
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)
# Example 2 ----------------------------------
# Keep rows where at least one treatment group in the row has at least 2 AEs observed
filter_hierarchical(tbl, n >= 2)
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.