Skip to contents

Frame-in / frame-out analogue of expand_trial(): takes an in-memory cohort data.frame and returns the expanded trial frame as a data.frame, with no intermediate Parquet. Wraps the extendr-generated expand_df().

Usage

expand_trial_df(
  cohort,
  id_col = "id",
  period_col = "period",
  treatment_col = "treatment",
  eligible_col = "eligible",
  outcome_col = "outcome",
  first_period = 0L,
  last_period = .Machine$integer.max,
  estimand = "ITT"
)

Arguments

cohort

A data.frame (or tibble / data.table / Arrow Table) of long person-time rows. Coerced with as.data.frame(), which preserves an integer64 column's class.

id_col, period_col, treatment_col, eligible_col, outcome_col

Column names. Defaults match the TrialEmulation conventions.

first_period, last_period

Inclusive integer period bounds.

estimand

"ITT" (intention-to-treat, no artificial censoring) or "PP" (per-protocol, censor each trial at the first treatment deviation).

Value

A data.frame with the six structural columns (id, trial_period, followup_time, assigned_treatment, treatment, outcome); an integer64 input column is returned as integer64.

Details

Column dtypes are preserved exactly: R integer <-> Polars Int32, double <-> Float64, and bit64::integer64 <-> Int64. A 64-bit integer column (e.g. a large id) round-trips exactly as integer64 with no precision loss above 2^53 (a pure-safe bit reinterpret, not a numeric cast).

See also

expand_trial() for the Parquet-path equivalent.

Examples

cohort <- data.frame(
  id = c(1L, 1L, 1L, 2L, 2L), period = c(0L, 1L, 2L, 0L, 1L),
  treatment = c(1L, 1L, 0L, 0L, 1L), eligible = c(1L, 0L, 0L, 1L, 0L),
  outcome = c(0L, 0L, 1L, 0L, 0L)
)
expand_trial_df(cohort, estimand = "ITT")
#>   id trial_period followup_time assigned_treatment treatment outcome
#> 1  1            0             0                  1         1       0
#> 2  1            0             1                  1         1       0
#> 3  1            0             2                  1         0       1
#> 4  2            0             0                  0         0       0
#> 5  2            0             1                  0         1       0