assign_timepoint: Assign a time point to a long data set with multiple measures

View source: R/assign_timepoint.R

assign_timepointR Documentation

Assign a time point to a long data set with multiple measures

Description

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.

Usage

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"
  )

ddsjoberg/bstfun documentation built on July 4, 2023, 10:59 a.m.