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
)
# 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"
)
# 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
)
# 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
)
Run the code above in your browser using DataLab