assign_timepoint: Assign a timepoint to a long dataset with multiple measures

Description Usage Arguments Value Examples

View source: R/assign_timepoint.R

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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))

ddsjoberg/hotfun documentation built on Dec. 12, 2021, 3:41 a.m.