Skip to contents

This function processes longitudinal data for exactly three waves (t0, t1, t2). It handles attrition, scales baseline variables, and optionally encodes ordinal variables. Note: This function is currently implemented for three waves only.

Usage

margot_process_longitudinal_data(
  df_wide,
  ordinal_columns = NULL,
  continuous_columns_keep = NULL
)

Arguments

df_wide

A wide-format dataframe containing longitudinal data for three waves.

ordinal_columns

A character vector of column names to be treated as ordinal and dummy-coded.

continuous_columns_keep

A character vector of continuous column names to keep without scaling.

Value

A processed dataframe suitable for use in longitudinal analyses with three waves.

Details

The function performs the following steps: 1. Creates NA conditions for t0 and t1 based on missingness in subsequent waves. 2. Handles non-factor and factor columns, applying attrition logic. 3. Scales numeric baseline (t0) variables. 4. Selects and orders columns. 5. Optionally encodes ordinal columns.

Note

This function is specifically designed for datasets with exactly three waves (t0, t1, t2). It may not work correctly for datasets with fewer or more waves.

Examples

# Assuming df_wide is your wide-format dataframe with three waves
processed_data <- margot_process_longitudinal_data(
  df_wide,
  ordinal_columns = c("education", "income_category"),
  continuous_columns_keep = c("age", "bmi")
)
#> 
#> ── Longitudinal Data Processing ────────────────────────────────────────────────
#>  Starting data processing for three waves (t0, t1, t2)
#> 
#> ── Step 1: Creating NA conditions ──
#> 
#> Error: object 'df_wide' not found