
Transform rowdep variable to rowind variable using first occurrence
Source:R/data_transformations.R
make_rowind_first_occurrence.RdCreates a row-independent (rowind) variable by finding the first occurrence where a condition is TRUE and extracting the corresponding value. This is a common pattern in longitudinal registry data analysis for creating stable person-level characteristics.
Details
This function implements the common pattern of transforming time-varying (rowdep) variables into time-invariant (rowind) variables by capturing the value at the first occurrence of a condition.
The transformation follows these steps: 1. Create temporary variable where condition is TRUE 2. Use first_non_na() to find the first occurrence for each person 3. Clean up temporary variables automatically
This is equivalent to the manual pattern:
dt[condition, temp := value_var]
dt[, new_var := first_non_na(temp), by = .(id)]
dt[, temp := NULL]
See also
first_non_na for the aggregation function used internally
Examples
if (FALSE) { # \dontrun{
# Create example skeleton with diagnosis data
skeleton <- create_skeleton(c(1,2,3), "2020-01-01", "2020-12-31")
# Add some example diagnosis data
add_diagnoses(skeleton, diagnosis_data, "lopnr",
diags = list("example_diag" = "^F64"))
# Transform: Age at first example diagnosis
make_rowind_first_occurrence(skeleton,
condition = "diag_example_diag == TRUE",
value_var = "age",
new_var = "rowind_age_first_example_diag")
} # }