This function helps user to find data rows which have datetime value of interest date_var closest to specified index_date value. User can use argument only_previous to handle if nearest row can be only before or also after the index_date date.

filter_date_nearest(.data, date_var, index_date, only_previous = FALSE)

Arguments

index_date

date or other lubridate variable as reference point around which to search

only_previous

optional parameter (default=FALSE): should we search only previously observed data prior index_date?

.date

data which contains datetime or other comparable variable

var

datetime or other comparable variable

Value

The row which is the nearest .date

See also

Other date functions: calc_age(), filter_date_first(), filter_date_last()

Examples

if (FALSE) { # searching for the first event of the each class (for each individual): d %>% group_by(personid) %>% filter_date_nearest(adm_date,index_date=postingdate) }