View source: R/adls_flatten_dates_dt.R
| FlattenDatesDT | R Documentation |
A data.table compatible function for simplifying time interval data
FlattenDatesDT(
data,
id,
in_date,
out_date,
status = NULL,
overlap_handling = c("none", "first", "most_recent"),
lag = 0
)
data |
A list, A data frame, or a data frame extension (e.g. a tibble) coercible to data.table. |
id |
One or more unquoted expression naming the id variables in data. |
in_date |
One unquoted expressions naming the start date variable in data. |
out_date |
One unquoted expression naming the end date variable in data. |
status |
One or more unquoted expressions naming a status variable in data, such as region or hospitalization reason. |
overlap_handling |
A character naming the method for handling overlaps
within an individuals time when
We currently don't have a method that lets the most recent status dominate and then potentially return to an older longer running status. If this is needed, please contact ADLS. |
lag |
A numeric, giving the number of days allowed between time intervals that should be collapsed into one. |
This functions identifies overlapping time intervals within individual and
collapses them into distinct and disjoint intervals. When status is
specified these intervals are both individual and status specific.
If lag is specified then intervals must be more then lag time units apart
to be considered distinct.
A data.table with the id, status if specified and simplified in_date
and out_date. The returned data is sorted by id and in_date.
ADLS, EMTH, ASO & KIJA
### The flatten function works with both dates and numeric
dat <- data.frame(
ID = c(1, 1, 1, 2, 2, 3, 3, 4),
START = c(1, 2, 5, 3, 6, 2, 3, 6),
END = c(3, 3, 7, 4, 9, 3, 5, 8))
dat |> FlattenDatesDT(ID, START, END)
dat <- data.frame(
ID = c(1, 1, 1, 2, 2, 3, 3, 4, 4),
START = as.Date(c("2012-02-15", "2005-12-13", "2006-01-24",
"2002-03-14", "1997-02-27",
"2008-08-13", "1998-09-23",
"2005-01-12", "2007-05-10")),
END = as.Date(c("2012-06-03", "2007-02-05", "2006-08-22",
"2005-02-26", "1999-04-16",
"2008-08-22", "2015-01-29",
"2007-05-07", "2008-12-12")))
dat |> FlattenDatesDT(ID, START, END)
### Allow for a 5 days lag between
dat |> FlattenDatesDT(ID, START, END, lag = 5)
### Adding status information
dat <- data.frame(
ID = c(1, 1, 1, 2, 2, 3, 3, 4, 4),
START = as.Date(c("2012-02-15", "2005-12-13", "2006-01-24",
"2002-03-14", "1997-02-27",
"2008-08-13", "1998-09-23",
"2005-01-12", "2007-05-10")),
END = as.Date(c("2012-06-03", "2007-02-05", "2006-08-22",
"2005-02-26", "1999-04-16",
"2008-08-22", "2015-01-29",
"2007-05-07", "2008-12-12")),
REGION = c("H", "H", "N", "S", "S", "M", "N", "S", "S"))
# Note the difference between the the different overlap_handling methods
dat |> FlattenDatesDT(ID, START, END, REGION, "none")
dat |> FlattenDatesDT(ID, START, END, REGION, "first")
dat |> FlattenDatesDT(ID, START, END, REGION, "most_recent")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.