Skip to contents

Creates a step that filters (subsets) rows from the survey data based on logical conditions. Multiple conditions are combined with AND.

Usage

step_filter(
  svy,
  ...,
  .by = NULL,
  .copy = use_copy_default(),
  comment = "Filter step",
  .level = "auto"
)

Arguments

svy

A Survey or RotativePanelSurvey object.

...

Logical expressions evaluated against the data. Each must return a logical vector. Multiple conditions are combined with AND.

.by

Optional grouping variable(s) for within-group filtering.

.copy

Whether to operate on a copy (default: use_copy_default()).

comment

Descriptive text for the step (default "Filter step").

.level

For RotativePanelSurvey, the level to apply (default "auto"): "implantation", "follow_up", or "auto" (both).

Value

The survey object with rows filtered and the step recorded.

Details

Lazy evaluation (default): Like all steps, filter is recorded but not executed until bake_steps() is called.

Examples

svy <- Survey$new(
  data = data.table::data.table(
    id = 1:10, age = c(15, 25, 35, 45, 55, 65, 75, 20, 30, 40), w = 1
  ),
  edition = "2023", type = "test", psu = NULL,
  engine = "data.table", weight = add_weight(annual = "w")
)
svy <- svy |> step_filter(age >= 18) |> bake_steps()
nrow(get_data(svy))
#> [1] 9