It’s all relative? – Calculating Relative Days using admiral
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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:
- 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( 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-04-30 11:34:09.568529
Details
Reuse
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} }
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.