forked from pharmaverse/admiral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderive_var_trtedtm.R
67 lines (66 loc) · 2.23 KB
/
derive_var_trtedtm.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#' Derive Datetime of Last Exposure to Treatment
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' This function is *deprecated*, please use `derive_vars_merged_dtm()` instead.
#'
#' Derives datetime of last exposure to treatment (`TRTEDTM`)
#'
#' @param dataset Input dataset
#'
#' The variables specified by the `by_vars` parameter are expected.
#'
#' @param dataset_ex `ex` dataset
#'
#' The variables `EXENDTC`, `EXSEQ`, and those specified by the `filter_ex`
#' parameter are expected.
#'
#' @param filter_ex Filter condition for the ex dataset
#'
#' Only observations of the ex dataset which fulfill the specified condition
#' are considered for the treatment start date.
#'
#' Default: `EXDOSE > 0 | (EXDOSE == 0 & str_detect(EXTRT, 'PLACEBO')) & nchar(EXENDTC) >= 10`
#'
#' Permitted Values: logical expression
#'
#' @param subject_keys Variables to uniquely identify a subject
#'
#' A list of quosures where the expressions are symbols as returned by
#' `vars()` is expected.
#'
#' @details For each group (with respect to the variables specified for the
#' `by_vars` parameter) the first observation (with respect to the order
#' specified for the `order` parameter) is included in the output dataset.
#'
#' @author Stefan Bundfuss
#'
#' @return The input dataset with `TRTEDTM` variable added
#'
#' @keywords adsl timing derivation
#'
#' @export
#'
derive_var_trtedtm <- function(dataset,
dataset_ex,
filter_ex = (EXDOSE > 0 | (EXDOSE == 0 & str_detect(EXTRT, "PLACEBO"))) & nchar(EXENDTC) >= 10, # nolint
subject_keys = vars(STUDYID, USUBJID)) {
assert_data_frame(dataset, subject_keys)
assert_data_frame(dataset_ex, required_vars = quo_c(subject_keys, vars(EXENDTC, EXSEQ)))
filter_ex <- assert_filter_cond(enquo(filter_ex), optional = TRUE)
deprecate_warn("0.7.0", "derive_var_trtedtm()", "derive_vars_merged_dtm()")
derive_vars_merged_dtm(
dataset,
dataset_add = dataset_ex,
filter_add = !!filter_ex,
new_vars_prefix = "TRTE",
dtc = EXENDTC,
date_imputation = "last",
time_imputation = "last",
flag_imputation = "none",
order = vars(TRTEDTM, EXSEQ),
mode = "last",
by_vars = subject_keys
)
}