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

ADaM
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)

adam <- tribble(
  ~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:

derive_vars_dy(
  adam,
  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(
  adam,
  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-11-15 19:40:02.323111

Details

Reuse

Citation

BibTeX citation:
@online{straub2023,
  author = {Straub, Ben},
  title = {It’s All Relative? - {Calculating} {Relative} {Days} Using
    Admiral},
  date = {2023-08-08},
  url = {https://pharmaverse.github.io/blog/posts/2023-08-08_study_day/study_day.html},
  langid = {en}
}
For attribution, please cite this work as:
Straub, Ben. 2023. “It’s All Relative? - Calculating Relative Days Using Admiral.” August 8, 2023. https://pharmaverse.github.io/blog/posts/2023-08-08_study_day/study_day.html.