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",
  keep_all_obs = FALSE,
  keep_all_vars = TRUE
)

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 time point to identify

windows

list of windows around a time point that are acceptable

time_units

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

new_var

name of new variable, default is "timepoint"

keep_all_obs

logical indicating whether to return a data frame with only the assigned time points (default), or to return a data frame with all rows.

keep_all_vars

logical indicating whether to return a data frame with all the variables in data= and the new time point column (default), or a limited data frame including only the column involved in assigning a time point.

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"
  )
#> # A tibble: 15 × 6
#>    date       variable    value value01 min_date   timepoint
#>    <date>     <chr>       <dbl>   <dbl> <date>         <dbl>
#>  1 1968-01-01 pce         531.  0.00207 1967-07-01         6
#>  2 1968-07-01 pce         563.  0.00483 1967-07-01        12
#>  3 1969-07-01 pce         603.  0.00821 1967-07-01        24
#>  4 1968-01-01 pop      199808   0.00901 1967-07-01         6
#>  5 1968-07-01 pop      200706   0.0164  1967-07-01        12
#>  6 1969-07-01 pop      202677   0.0326  1967-07-01        24
#>  7 1968-01-01 psavert      11.7 0.629   1967-07-01         6
#>  8 1968-07-01 psavert      10.7 0.563   1967-07-01        12
#>  9 1969-07-01 psavert      11.8 0.636   1967-07-01        24
#> 10 1968-01-01 uempmed       5.1 0.0519  1967-07-01         6
#> 11 1968-07-01 uempmed       4.5 0.0236  1967-07-01        12
#> 12 1969-07-01 uempmed       4.4 0.0189  1967-07-01        24
#> 13 1968-01-01 unemploy   2878   0.0152  1967-07-01         6
#> 14 1968-07-01 unemploy   2883   0.0156  1967-07-01        12
#> 15 1969-07-01 unemploy   2868   0.0144  1967-07-01        24