Pivot a rollout schedule from wide to long format with local time calculation
Source:R/data_generation.R
pivot_schedule_longer.Rd
Transforms a wide-format rollout schedule into a long-format schedule, extracting chronological time from column names, converting condition columns to factors, and adding local time within each cohort if desired.
Usage
pivot_schedule_longer(
schedule,
time_cols,
names_to = "chron_time",
names_pattern = ".*(\\d+)",
names_transform = as.numeric,
values_to = "condition",
values_transform = as.factor,
cohort_name = cohort,
local_time = TRUE
)
Arguments
- schedule
A data frame containing the rollout schedule in wide format.
- time_cols
Columns containing time-specific condition assignments (tidyselect syntax).
- names_to
Name of the new column to store extracted chronological time (default
"chron_time"
).- names_pattern
Regular expression to extract the numeric time from column names (default
".*(\\d+)"
).- names_transform
Function to transform extracted time values (default
as.numeric
).- values_to
Name of the new column to store condition values (default
"condition"
).- values_transform
Function to transform condition values (default
as.factor
).- cohort_name
The column indicating cohort membership for local time calculation (default
cohort
).- local_time
Logical; if
TRUE
, adds alocal_time
column indicating time since rollout start for each cohort and condition (defaultTRUE
).
Value
A long-format tibble
with columns for cohort, condition, chronological time, and optionally local time.
Examples
library(dplyr)
library(tidyr)
#>
#> Attaching package: ‘tidyr’
#> The following objects are masked from ‘package:Matrix’:
#>
#> expand, pack, unpack
schedule <- tibble::tibble(
site = c("A", "B"),
cohort = c(1, 2),
t1 = c("control", "intervention"),
t2 = c("intervention", "intervention")
)
pivot_schedule_longer(schedule, time_cols = starts_with("t"))
#> # A tibble: 4 × 5
#> site cohort chron_time condition local_time
#> <chr> <dbl> <dbl> <fct> <dbl>
#> 1 A 1 1 control 0
#> 2 A 1 2 intervention 0
#> 3 B 2 1 intervention 0
#> 4 B 2 2 intervention 1