Skip to contents

Collapses long-format medication data (cycles 3-6) to one row per respondent by taking the maximum value of each medication variable across all rows. A result of 1 (taking the medication) takes precedence over 0. Tagged NAs are propagated only when all rows for a respondent are missing.

Usage

aggregate_meds_by_person(data, variables, by = "clinicid")

Arguments

data

data.frame Long-format medication data with multiple rows per respondent.

variables

character Variable names to aggregate.

by

character The respondent identifier column. Default is "clinicid".

Value

data.frame One row per respondent with aggregated medication variables as numeric.

Examples

df <- data.frame(
  clinicid    = c(1, 1, 2, 2),
  any_htn_med = c(0, 1, 0, 0),
  diab_med    = c(1, 0, 0, 0)
)
aggregate_meds_by_person(df, variables = c("any_htn_med", "diab_med"))
#> # A tibble: 2 × 3
#>   clinicid any_htn_med diab_med
#>      <dbl>       <dbl>    <dbl>
#> 1        1           1        1
#> 2        2           0        0