| derive_vars_extreme_event {admiral} | R Documentation |
Add the Worst or Best Observation for Each By Group as New Variables
Description
Add the first available record from events for each by group as new
variables, all variables of the selected observation are kept. It can be used
for selecting the extreme observation from a series of user-defined events.
Usage
derive_vars_extreme_event(
dataset,
by_vars,
events,
tmp_event_nr_var = NULL,
order,
mode,
source_datasets = NULL,
check_type = "warning",
new_vars
)
Arguments
dataset |
Input dataset The variables specified by the |
by_vars |
Grouping variables Default: Permitted Values: list of variables created by |
events |
Conditions and new values defining events A list of For |
tmp_event_nr_var |
Temporary event number variable The specified variable is added to all source datasets and is set to the number of the event before selecting the records of the event. It can be used in The variable is not included in the output dataset. |
order |
Sort order If a particular event from For handling of Permitted Values: list of expressions created by |
mode |
Selection mode (first or last) If a particular event from Permitted Values: |
source_datasets |
Source datasets A named list of datasets is expected. The |
check_type |
Check uniqueness? If Default: Permitted Values: |
new_vars |
Variables to add The specified variables from the events are added to the output
dataset. Variables can be renamed by naming the element, i.e., |
Details
For each event select the observations to consider:
If the event is of class
event, the observations of the source dataset are restricted byconditionand then the first or last (mode) observation per by group (by_vars) is selected.If the event is of class
event_joined,filter_joined()is called to select the observations.The variables specified by the
set_values_tofield of the event are added to the selected observations.The variable specified for
tmp_event_nr_varis added and set to the number of the event.
All selected observations are bound together.
For each group (with respect to the variables specified for the
by_varsparameter) the first or last observation (with respect to the order specified for theorderparameter and the mode specified for themodeparameter) is selected.The variables specified by the
new_varsparameter are added to the selected observations.The variables are added to input dataset.
Value
The input dataset with the best or worst observation of each by group added as new variables.
See Also
event(), event_joined(), derive_extreme_event()
ADSL Functions that returns variable appended to dataset:
derive_var_age_years(),
derive_vars_aage(),
derive_vars_period()
Examples
library(tibble)
library(dplyr)
library(lubridate)
adsl <- tribble(
~STUDYID, ~USUBJID, ~TRTEDT, ~DTHDT,
"PILOT01", "01-1130", ymd("2014-08-16"), ymd("2014-09-13"),
"PILOT01", "01-1133", ymd("2013-04-28"), ymd(""),
"PILOT01", "01-1211", ymd("2013-01-12"), ymd(""),
"PILOT01", "09-1081", ymd("2014-04-27"), ymd(""),
"PILOT01", "09-1088", ymd("2014-10-09"), ymd("2014-11-01"),
)
lb <- tribble(
~STUDYID, ~DOMAIN, ~USUBJID, ~LBSEQ, ~LBDTC,
"PILOT01", "LB", "01-1130", 219, "2014-06-07T13:20",
"PILOT01", "LB", "01-1130", 322, "2014-08-16T13:10",
"PILOT01", "LB", "01-1133", 268, "2013-04-18T15:30",
"PILOT01", "LB", "01-1133", 304, "2013-05-01T10:13",
"PILOT01", "LB", "01-1211", 8, "2012-10-30T14:26",
"PILOT01", "LB", "01-1211", 162, "2013-01-08T12:13",
"PILOT01", "LB", "09-1081", 47, "2014-02-01T10:55",
"PILOT01", "LB", "09-1081", 219, "2014-05-10T11:15",
"PILOT01", "LB", "09-1088", 283, "2014-09-27T12:13",
"PILOT01", "LB", "09-1088", 322, "2014-10-09T13:25"
) %>%
mutate(
ADT = convert_dtc_to_dt(LBDTC)
)
derive_vars_extreme_event(
adsl,
by_vars = exprs(STUDYID, USUBJID),
events = list(
event(
dataset_name = "adsl",
condition = !is.na(DTHDT),
set_values_to = exprs(LSTALVDT = DTHDT, DTHFL = "Y")
),
event(
dataset_name = "lb",
condition = !is.na(ADT),
order = exprs(ADT),
mode = "last",
set_values_to = exprs(LSTALVDT = ADT, DTHFL = "N")
),
event(
dataset_name = "adsl",
condition = !is.na(TRTEDT),
order = exprs(TRTEDT),
mode = "last",
set_values_to = exprs(LSTALVDT = TRTEDT, DTHFL = "N")
)
),
source_datasets = list(adsl = adsl, lb = lb),
tmp_event_nr_var = event_nr,
order = exprs(LSTALVDT, event_nr),
mode = "last",
new_vars = exprs(LSTALVDT, DTHFL)
)
# Derive DTHCAUS from AE and DS domain data
adsl <- tribble(
~STUDYID, ~USUBJID,
"STUDY01", "PAT01",
"STUDY01", "PAT02",
"STUDY01", "PAT03"
)
ae <- tribble(
~STUDYID, ~USUBJID, ~AESEQ, ~AEDECOD, ~AEOUT, ~AEDTHDTC,
"STUDY01", "PAT01", 12, "SUDDEN DEATH", "FATAL", "2021-04-04",
"STUDY01", "PAT01", 13, "CARDIAC ARREST", "FATAL", "2021-04-03",
)
ds <- tribble(
~STUDYID, ~USUBJID, ~DSSEQ, ~DSDECOD, ~DSTERM, ~DSSTDTC,
"STUDY01", "PAT02", 1, "INFORMED CONSENT OBTAINED", "INFORMED CONSENT OBTAINED", "2021-04-03",
"STUDY01", "PAT02", 2, "RANDOMIZATION", "RANDOMIZATION", "2021-04-11",
"STUDY01", "PAT02", 3, "DEATH", "DEATH DUE TO PROGRESSION OF DISEASE", "2022-02-01",
"STUDY01", "PAT03", 1, "DEATH", "POST STUDY REPORTING OF DEATH", "2022-03-03"
)
derive_vars_extreme_event(
adsl,
by_vars = exprs(STUDYID, USUBJID),
events = list(
event(
dataset_name = "ae",
condition = AEOUT == "FATAL",
set_values_to = exprs(DTHCAUS = AEDECOD, DTHDT = convert_dtc_to_dt(AEDTHDTC)),
order = exprs(DTHDT)
),
event(
dataset_name = "ds",
condition = DSDECOD == "DEATH" & grepl("DEATH DUE TO", DSTERM),
set_values_to = exprs(DTHCAUS = DSTERM, DTHDT = convert_dtc_to_dt(DSSTDTC)),
order = exprs(DTHDT)
)
),
source_datasets = list(ae = ae, ds = ds),
tmp_event_nr_var = event_nr,
order = exprs(DTHDT, event_nr),
mode = "first",
new_vars = exprs(DTHCAUS, DTHDT)
)