if_date {healthdb} | R Documentation |
Interpret if any n elements drawn from a date vector could be some days apart within some time span
Description
Given a vector of dates x, interpret if there could be at least one set of n elements taken from x satisfy that adjacent elements in the set are at least certain days apart AND the dates in the set are within the specified time span. When identifying events/diseases from administrative data, definitions often require, e.g., n diagnoses that are at least some days apart within some years. This function is intended for such use and optimized to avoid looping through all n-size combinations in x. This function does not work with remote table input.
Usage
if_date(
x,
n,
apart = NULL,
within = NULL,
detail = FALSE,
align = c("left", "right"),
dup.rm = TRUE,
...
)
Arguments
x |
A character or Date vector |
n |
An integer for the size of a draw |
apart |
An integer specifying the minimum gap (in days) between adjacent dates in a draw. |
within |
An integer specifying the maximum time span (in days) of a draw. |
detail |
Logical for whether return result per element of x.The default is FALSE, which returns one logical summarized by any(). Detail is not available if |
align |
Character, define if the time span for each record should start ("left") or end ("right") at its current date. Defaults to "left". See 'flag_at' argument in |
dup.rm |
Logical for whether multiple records on the same date should be count as one in calculation. Only applicable when |
... |
Additional argument passing to |
Value
Single or a vector of logical for whether there is any draw from x satisfied the conditions
See Also
Examples
dates_of_records <- sample(seq(as.Date("2015-01-01"), as.Date("2021-12-31"), 7), 10)
# whether there is any 3 records at least 30 days apart within 2 years
if_date(dates_of_records, n = 3, apart = 30, within = 365 * 2)
# specified either apart or within or both
if_date(dates_of_records, n = 2, within = 365)