library(tibble)
library(dplyr, warn.conflicts = FALSE)
library(lubridate)
adlb <- tribble(
~USUBJID, ~AVISITN, ~AVAL, ~LBSEQ,
"1", 1, 113, 1,
"1", 2, 113, 2,
"1", 3, 117, 3,
"2", 1, 101, 1,
"2", 2, 101, 2,
"2", 3, 95, 3
)
# Add a new record for each USUBJID storing the minimum value (first AVAL).
# If multiple records meet the minimum criterion, take the first value by
# AVISITN. Set AVISITN = 97 and DTYPE = MINIMUM for these new records.
# Specify the variables that need to be kept in the new records.
derive_extreme_records(
adlb,
dataset_add = adlb,
by_vars = exprs(USUBJID),
order = exprs(AVAL, AVISITN),
mode = "first",
filter_add = !is.na(AVAL),
keep_source_vars = exprs(AVAL),
set_values_to = exprs(
AVISITN = 97,
DTYPE = "MINIMUM"
)
)
# Add a new record for each USUBJID storing the maximum value (last AVAL).
# If multiple records meet the maximum criterion, take the first value by
# AVISITN. Set AVISITN = 98 and DTYPE = MAXIMUM for these new records.
derive_extreme_records(
adlb,
dataset_add = adlb,
by_vars = exprs(USUBJID),
order = exprs(desc(AVAL), AVISITN),
mode = "first",
filter_add = !is.na(AVAL),
set_values_to = exprs(
AVISITN = 98,
DTYPE = "MAXIMUM"
)
)
# Add a new record for each USUBJID storing for the last value.
# Set AVISITN = 99 and DTYPE = LOV for these new records.
derive_extreme_records(
adlb,
dataset_add = adlb,
by_vars = exprs(USUBJID),
order = exprs(AVISITN),
mode = "last",
set_values_to = exprs(
AVISITN = 99,
DTYPE = "LOV"
)
)
# Derive a new parameter for the first disease progression (PD)
adsl <- tribble(
~USUBJID, ~DTHDT,
"1", ymd("2022-05-13"),
"2", ymd(""),
"3", ymd("")
) %>%
mutate(STUDYID = "XX1234")
adrs <- tribble(
~USUBJID, ~ADTC, ~AVALC,
"1", "2020-01-02", "PR",
"1", "2020-02-01", "CR",
"1", "2020-03-01", "CR",
"1", "2020-04-01", "SD",
"2", "2021-06-15", "SD",
"2", "2021-07-16", "PD",
"2", "2021-09-14", "PD"
) %>%
mutate(
STUDYID = "XX1234",
ADT = ymd(ADTC),
PARAMCD = "OVR",
PARAM = "Overall Response",
ANL01FL = "Y"
) %>%
select(-ADTC)
derive_extreme_records(
adrs,
dataset_ref = adsl,
dataset_add = adrs,
by_vars = exprs(STUDYID, USUBJID),
filter_add = PARAMCD == "OVR" & AVALC == "PD",
order = exprs(ADT),
exist_flag = AVALC,
true_value = "Y",
false_value = "N",
mode = "first",
set_values_to = exprs(
PARAMCD = "PD",
PARAM = "Disease Progression",
AVAL = yn_to_numeric(AVALC),
ANL01FL = "Y",
ADT = ADT
)
)
# derive parameter indicating death
derive_extreme_records(
dataset_ref = adsl,
dataset_add = adsl,
by_vars = exprs(STUDYID, USUBJID),
filter_add = !is.na(DTHDT),
exist_flag = AVALC,
true_value = "Y",
false_value = "N",
mode = "first",
set_values_to = exprs(
PARAMCD = "DEATH",
PARAM = "Death",
ANL01FL = "Y",
ADT = DTHDT
)
)
Run the code above in your browser using DataLab