View source: R/assign_timepoint.R
assign_timepoint | R Documentation |
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 )
data |
data frame |
id |
id variable name, such as |
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 |
new_var |
name of new variable, default is |
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 frame passed in data
with additional column new_var
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" )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.