Given a data set that has a measure collected over time and you want to extract, for example the 3 month measurement, this function will find the measure closest to 3 months within a defined window.

assign_timepoint(
  data,
  id,
  ref_date,
  measure_date,
  timepoints,
  windows,
  time_units = c("days", "weeks", "months", "years"),
  new_var = "timepoint"
)

Arguments

data

data frame

id

id variable name, such as "mrn"

ref_date

baseline or reference date column name

measure_date

date the measure was collected

timepoints

vector of timepoint to identify

windows

list of windows around a timepoint that are acceptable

time_units

one of c("days", "weeks", "months", "years")

new_var

name of new variable, default is "timepoint"

Value

data frame passed in data with additional column new_var

Examples

ggplot2::economics_long %>%
  dplyr::group_by(variable) %>%
  dplyr::mutate(min_date = min(date)) %>%
  dplyr::ungroup() %>%
  assign_timepoint(
    id = "variable",
    ref_date = "min_date",
    measure_date = "date",
    timepoints = c(6, 12, 24),
    windows = list(c(-2, 2), c(-2, 2), c(-2, 2)),
    time_units = "months"
  ) %>%
  dplyr::filter(!is.na(timepoint))
#> # A tibble: 15 × 4
#>    variable min_date   date       timepoint
#>    <chr>    <date>     <date>         <dbl>
#>  1 pce      1967-07-01 1968-01-01         6
#>  2 pce      1967-07-01 1968-07-01        12
#>  3 pce      1967-07-01 1969-07-01        24
#>  4 pop      1967-07-01 1968-01-01         6
#>  5 pop      1967-07-01 1968-07-01        12
#>  6 pop      1967-07-01 1969-07-01        24
#>  7 psavert  1967-07-01 1968-01-01         6
#>  8 psavert  1967-07-01 1968-07-01        12
#>  9 psavert  1967-07-01 1969-07-01        24
#> 10 uempmed  1967-07-01 1968-01-01         6
#> 11 uempmed  1967-07-01 1968-07-01        12
#> 12 uempmed  1967-07-01 1969-07-01        24
#> 13 unemploy 1967-07-01 1968-01-01         6
#> 14 unemploy 1967-07-01 1968-07-01        12
#> 15 unemploy 1967-07-01 1969-07-01        24