trim_events: Trim longitudinal records at an event occurrence

View source: R/sequences.R

trim_eventsR Documentation

Trim longitudinal records at an event occurrence

Description

Trim longitudinal records at an event occurrence

Usage

trim_events(data, id, period, event, occurrence = 1L, event_value = 1)

Arguments

data

Long-form base data.frame.

id

Unit identifier.

period

Ordered period variable.

event

Event indicator variable.

occurrence

Event occurrence at which to stop.

event_value

Value identifying an event.

Value

A simulab_sim base data.frame retaining observations through the requested event, or all observations when it never occurs.

Examples

# A long-form event log: `simulate_until_event()` generates the indicator,
# `trim_events()` then keeps records through its first occurrence.
data <- simulate_until_event(
  expand_periods(data.frame(id = 1:20), periods = 10,
                 id = "id", period = "period"),
  definition = define_variable("event", formula = "0.25",
                               distribution = "binary"),
  seed = 1
)

result <- trim_events(
  data, id = "id", period = "period", event = "event", occurrence = 1
)
head(result)

simulab documentation built on Oct. 15, 2026, 5:08 p.m.