Derives duration between two dates, specified by the variables present in input dataset e.g., duration of adverse events, relative day, age, ...
Usage
derive_vars_duration(
dataset,
new_var,
new_var_unit = NULL,
start_date,
end_date,
in_unit = "days",
out_unit = "days",
floor_in = TRUE,
add_one = TRUE,
trunc_out = FALSE
)
Arguments
- dataset
Input dataset
The variables specified by the
start_date
and theend_date
parameter are expected.- new_var
Name of variable to create
- new_var_unit
Name of the unit variable If the parameter is not specified, no variable for the unit is created.
- start_date
The start date
A date or date-time variable is expected. This variable must be present in specified input dataset.
Refer to
derive_vars_dt()
to impute and derive a date from a date character vector to a date object.- end_date
The end date
A date or date-time variable is expected. This variable must be present in specified input dataset.
Refer to
derive_vars_dt()
to impute and derive a date from a date character vector to a date object.- in_unit
Input unit
See floor_in and add_one parameter for details.
Default: 'days'
Permitted Values: 'years', 'months', 'days', 'hours', 'minutes', 'min', 'seconds', 'sec'
- out_unit
Output unit
The duration is derived in the specified unit
Default: 'days'
Permitted Values: 'years', 'months', 'days', 'hours', 'minutes', 'min', 'seconds', 'sec'
- floor_in
Round down input dates?
The input dates are round down with respect to the input unit, e.g., if the input unit is 'days', the time of the input dates is ignored.
Default: `TRUE``
Permitted Values:
TRUE
,FALSE
- add_one
Add one input unit?
If the duration is non-negative, one input unit is added. I.e., the duration can not be zero.
Default:
TRUE
Permitted Values:TRUE
,FALSE
- trunc_out
Return integer part
The fractional part of the duration (in output unit) is removed, i.e., the integer part is returned.
Default:
FALSE
Permitted Values:
TRUE
,FALSE
Details
The duration is derived as time from start to end date in the specified output unit. If the end date is before the start date, the duration is negative. The start and end date variable must be present in the specified input dataset.
See also
Date/Time Derivation Functions that returns variable appended to dataset:
derive_var_trtdurd()
,
derive_vars_dtm_to_dt()
,
derive_vars_dtm_to_tm()
,
derive_vars_dtm()
,
derive_vars_dt()
,
derive_vars_dy()
Examples
library(lubridate)
library(tibble)
# Derive age in years
data <- tribble(
~USUBJID, ~BRTHDT, ~RANDDT,
"P01", ymd("1984-09-06"), ymd("2020-02-24"),
"P02", ymd("1985-01-01"), NA,
"P03", NA, ymd("2021-03-10"),
"P04", NA, NA
)
derive_vars_duration(data,
new_var = AAGE,
new_var_unit = AAGEU,
start_date = BRTHDT,
end_date = RANDDT,
out_unit = "years",
add_one = FALSE,
trunc_out = TRUE
)
#> # A tibble: 4 x 5
#> USUBJID BRTHDT RANDDT AAGE AAGEU
#> <chr> <date> <date> <dbl> <chr>
#> 1 P01 1984-09-06 2020-02-24 35 YEARS
#> 2 P02 1985-01-01 NA NA NA
#> 3 P03 NA 2021-03-10 NA NA
#> 4 P04 NA NA NA NA
# Derive adverse event duration in days
data <- tribble(
~USUBJID, ~ASTDT, ~AENDT,
"P01", ymd("2021-03-05"), ymd("2021-03-02"),
"P02", ymd("2019-09-18"), ymd("2019-09-18"),
"P03", ymd("1985-01-01"), NA,
"P04", NA, NA
)
derive_vars_duration(data,
new_var = ADURN,
new_var_unit = ADURU,
start_date = ASTDT,
end_date = AENDT,
out_unit = "days"
)
#> # A tibble: 4 x 5
#> USUBJID ASTDT AENDT ADURN ADURU
#> <chr> <date> <date> <dbl> <chr>
#> 1 P01 2021-03-05 2021-03-02 -3 DAYS
#> 2 P02 2019-09-18 2019-09-18 1 DAYS
#> 3 P03 1985-01-01 NA NA NA
#> 4 P04 NA NA NA NA
# Derive adverse event duration in minutes
data <- tribble(
~USUBJID, ~ADTM, ~TRTSDTM,
"P01", ymd_hms("2019-08-09T04:30:56"), ymd_hms("2019-08-09T05:00:00"),
"P02", ymd_hms("2019-11-11T10:30:00"), ymd_hms("2019-11-11T11:30:00"),
"P03", ymd_hms("2019-11-11T00:00:00"), ymd_hms("2019-11-11T04:00:00"),
"P04", NA, ymd_hms("2019-11-11T12:34:56"),
)
derive_vars_duration(data,
new_var = ADURN,
new_var_unit = ADURU,
start_date = ADTM,
end_date = TRTSDTM,
in_unit = "minutes",
out_unit = "minutes",
add_one = FALSE
)
#> # A tibble: 4 x 5
#> USUBJID ADTM TRTSDTM ADURN ADURU
#> <chr> <dttm> <dttm> <dbl> <chr>
#> 1 P01 2019-08-09 04:30:56 2019-08-09 05:00:00 30 MINUTES
#> 2 P02 2019-11-11 10:30:00 2019-11-11 11:30:00 60 MINUTES
#> 3 P03 2019-11-11 00:00:00 2019-11-11 04:00:00 240 MINUTES
#> 4 P04 NA 2019-11-11 12:34:56 NA NA
# Derive adverse event start time since last dose in hours
data <- tribble(
~USUBJID, ~ASTDTM, ~LDOSEDTM,
"P01", ymd_hms("2019-08-09T04:30:56"), ymd_hms("2019-08-08T10:05:00"),
"P02", ymd_hms("2019-11-11T23:59:59"), ymd_hms("2019-10-11T11:37:00"),
"P03", ymd_hms("2019-11-11T00:00:00"), ymd_hms("2019-11-10T23:59:59"),
"P04", ymd_hms("2019-11-11T12:34:56"), NA,
"P05", NA, ymd_hms("2019-09-28T12:34:56")
)
derive_vars_duration(
data,
new_var = LDRELTM,
new_var_unit = LDRELTMU,
start_date = LDOSEDTM,
end_date = ASTDTM,
in_unit = "hours",
out_unit = "hours",
add_one = FALSE
)
#> # A tibble: 5 x 5
#> USUBJID ASTDTM LDOSEDTM LDRELTM LDRELTMU
#> <chr> <dttm> <dttm> <dbl> <chr>
#> 1 P01 2019-08-09 04:30:56 2019-08-08 10:05:00 18 HOURS
#> 2 P02 2019-11-11 23:59:59 2019-10-11 11:37:00 756 HOURS
#> 3 P03 2019-11-11 00:00:00 2019-11-10 23:59:59 1 HOURS
#> 4 P04 2019-11-11 12:34:56 NA NA NA
#> 5 P05 NA 2019-09-28 12:34:56 NA NA