It’s all relative? - Calculating Relative Days using admiral

Author

Ben Straub

Published

August 8, 2023

Creating `--DY` variables for your ADaMs is super easy using `derive_vars_dy()` from the admiral package.

Let’s build some dummy data with 4 subjects, a start date/time for treatment (`TRTSDTM`), an analysis start date/time variable (`ASTDTM`) and an analysis end date variable (`AENDT`).

``````library(admiral)
library(lubridate)
library(dplyr)

~USUBJID, ~TRTSDTM, ~ASTDTM, ~AENDT,
"001", "2014-01-17T23:59:59", "2014-01-18T13:09:O9", "2014-01-20",
"002", "2014-02-25T23:59:59", "2014-03-18T14:09:O9", "2014-03-24",
"003", "2014-02-12T23:59:59", "2014-02-18T11:03:O9", "2014-04-17",
"004", "2014-03-17T23:59:59", "2014-03-19T13:09:O9", "2014-05-04"
) %>%
mutate(
TRTSDTM = as_datetime(TRTSDTM),
ASTDTM = as_datetime(ASTDTM),
AENDT = ymd(AENDT)
)``````

Okay! Next we run our dataset through `derive_vars_dy()`, specifying:

• the reference date of the variable we want the calculation to be done against (`reference_date` argument);
• the variables required to help calculate the relative days (`source_vars` argument).
``````derive_vars_dy(
reference_date = TRTSDTM,
source_vars = exprs(ASTDTM, AENDT)
)``````
``````# A tibble: 4 × 6
USUBJID TRTSDTM             ASTDTM              AENDT      ASTDY AENDY
<chr>   <dttm>              <dttm>              <date>     <dbl> <dbl>
1 001     2014-01-17 23:59:59 2014-01-18 13:09:09 2014-01-20     2     4
2 002     2014-02-25 23:59:59 2014-03-18 14:09:09 2014-03-24    22    28
3 003     2014-02-12 23:59:59 2014-02-18 11:03:09 2014-04-17     7    65
4 004     2014-03-17 23:59:59 2014-03-19 13:09:09 2014-05-04     3    49``````

That’s it! We got both our `ASTDY` and `AENDY` variables in only a few short lines of code!

What if I want my variables to have a different naming convention?

Easy! In the `source_vars` argument if you want your variables to be called `DEMOADY` and `DEMOEDY` just do `DEMOADY = ASTDTM` and `DEMOEDY = AENDT` and `derive_vars_dy()` will do the rest!

``````derive_vars_dy(
reference_date = TRTSDTM,
source_vars = exprs(DEMOADY = ASTDTM, DEMOEDY = AENDT)
)``````
``````# A tibble: 4 × 6
USUBJID TRTSDTM             ASTDTM              AENDT      DEMOADY DEMOEDY
<chr>   <dttm>              <dttm>              <date>       <dbl>   <dbl>
1 001     2014-01-17 23:59:59 2014-01-18 13:09:09 2014-01-20       2       4
2 002     2014-02-25 23:59:59 2014-03-18 14:09:09 2014-03-24      22      28
3 003     2014-02-12 23:59:59 2014-02-18 11:03:09 2014-04-17       7      65
4 004     2014-03-17 23:59:59 2014-03-19 13:09:09 2014-05-04       3      49``````

If you want to get `--DT` or `--DTM` variables using `admiral` then check out `derive_vars_dt()` and `derive_vars_dtm()`. If things are messy in your data, e.g. partial dates, both functions have great imputation abilities, which we will cover in an upcoming blog post!

Last updated

2024-08-07 14:00:22.809859

Citation

BibTeX citation:
``````@online{straub2023,
author = {Straub, Ben},
title = {It’s All Relative? - {Calculating} {Relative} {Days} Using