Skip to contents

Keeps the rows of a tbl_hierarchical() or tbl_hierarchical_count() table whose statistics satisfy an expression, e.g. filter_hierarchical(x, n > 5). The expression is evaluated once per row of the target variable (var, by default the innermost one), where these names are available:

  • n, N, p: the row's statistics, as vectors across the table's columns, so n > 5 keeps rows where any column's count exceeds 5 (tbl_hierarchical_count() tables only have n);

  • the by variable's values under its own name, e.g. n > 5 & trt == "B";

  • n_1, p_2, ...: one column's statistic, by column position;

  • n_overall, N_overall, p_overall: sums across the columns.

A section whose rows are all removed loses its summary row too, unless keep_empty = TRUE. Filtering on an outer variable removes failing sections whole. The overall row always stays.

Usage

filter_hierarchical(
  x,
  filter,
  var = NULL,
  keep_empty = FALSE,
  quiet = FALSE,
  ...
)

Arguments

x

(tbl_hierarchical, tbl_hierarchical_count)
A hierarchical table.

filter

(expression)
An unquoted filter expression.

var

(selector)
The hierarchy variable whose rows the expression tests. Default is the last include variable.

keep_empty

(scalar logical)
Whether emptied sections keep their summary rows. Default is FALSE.

quiet

(scalar logical)
Whether to suppress the message naming the column of each indexed statistic. Default is FALSE.

...

Not used.

Value

The table with the failing rows removed.

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))
tbl <- tbl_hierarchical(ae, variables = c(soc, term), by = trt,
                        denominator = enrolled, id = id)

# Example 1 ----------------------------------
# keep the terms at least two subjects of a column report
filter_hierarchical(tbl, n > 1)
# Example 2 ---------------------------------- # keep the terms 40% of all subjects report filter_hierarchical(tbl, p_overall > 0.4)