generated from pharmaverse/admiraltemplate
-
Notifications
You must be signed in to change notification settings - Fork 8
/
derive_vars_crit.R
146 lines (143 loc) · 4.62 KB
/
derive_vars_crit.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#' Derive Analysis Criterion Evaluation Variables
#'
#'
#' @description
#' Derive analysis criterion evaluation result variable, paired with character
#' and numeric flags.
#' This function allows also the derivation of a CRIT like variable with a
#' different name (ex: `ANL01FL`), without generating additional numeric (ex: `ANL01FN`)
#' and character label (ex: `ANL01`) variables.
#'
#' @param dataset Input dataset
#'
#'
#' @param prefix Variables to add
#'
#' The analysis criterion evaluation variable's name (i.e., `CRIT1`)
#' This name is also used in order to create both character and numeric
#' flags variables (i.e., `CRIT1FL` and `CRIT1FN`).
#' If the name does not contain CRIT wording, it generates a flag variable
#' (ex: `ANL01FL`) whose logic is equals to `CRIT1` variable, without generating
#' additional numeric (ex: `ANL01FN`) and character (`ANL01`) variables.
#'
#'
#' @param crit_label Criterion value
#'
#' A text description defining the condition necessary to satisfy the presence
#' of the criterion
#'
#'
#' @param condition Condition for selecting a subset
#'
#' The condition specified in order to select a subset from the input dataset
#' in which the rule is applied.
#'
#'
#' @param criterion Criterion rule
#'
#' The criterion that each selected row satisfies or not.
#' Returns `Y` or `N` for character variable and `1` or `0` for numeric variable
#' if the criterion is met or not, respectively.
#' Returns `NA` for not selected rows (not taken into account from condition)
#'
#'
#' @return Dataset with criterion variables
#'
#' @export
#'
#' @author Federico Baratin
#' @export
#' @keywords der_var
#' @family der_var
#'
#' @examples
#' library(tibble)
#' library(admiral)
#' library(admiraldev)
#' library(dplyr)
#'
#' input <- tribble(
#' ~USUBJID, ~AVISITN, ~ISCAT, ~PARAMCD, ~AVAL, ~ISLLOQ,
#' "999999-000001", 10, "IMMUNOLOGY", "J0033VN", 2, 4,
#' "999999-000001", 10, "IMMUNOLOGY", "I0019NT", 3, 6,
#' "999999-000001", 10, "IMMUNOLOGY", "M0019LN", 4, 4,
#' "999999-000001", 10, "IMMUNOLOGY", "R0003MA", 3, 6,
#' "999999-000001", 30, "IMMUNOLOGY", "J0033VN", 60, 4,
#' "999999-000001", 30, "IMMUNOLOGY", "I0019NT", 567, 6,
#' "999999-000001", 30, "IMMUNOLOGY", "M0019LN", 659, 4,
#' "999999-000001", 30, "IMMUNOLOGY", "R0003MA", 250, 6,
#' "999999-000002", 10, "IMMUNOLOGY", "J0033VN", 2, 4,
#' "999999-000002", 10, "IMMUNOLOGY", "I0019NT", 7, 6,
#' "999999-000002", 10, "IMMUNOLOGY", "M0019LN", 5, 4,
#' "999999-000002", 10, "IMMUNOLOGY", "R0003MA", 3, 6,
#' "999999-000002", 30, "IMMUNOLOGY", "J0033VN", 55, 4,
#' "999999-000002", 30, "IMMUNOLOGY", "I0019NT", 89, 6,
#' "999999-000002", 30, "IMMUNOLOGY", "M0019LN", 990, 4,
#' "999999-000002", 30, "IMMUNOLOGY", "R0003MA", 340, 6,
#' "999999-000003", 10, "IMMUNOLOGY", "J0033VN", 3, 4,
#' "999999-000003", 10, "IMMUNOLOGY", "I0019NT", 6, 6,
#' "999999-000003", 10, "IMMUNOLOGY", "M0019LN", 2, 4,
#' "999999-000003", 10, "IMMUNOLOGY", "R0003MA", 2, 6,
#' "999999-000003", 30, "IMMUNOLOGY", "J0033VN", 45, 4,
#' "999999-000003", 30, "IMMUNOLOGY", "I0019NT", 381, 6,
#' "999999-000003", 30, "IMMUNOLOGY", "M0019LN", 542, 4,
#' "999999-000003", 30, "IMMUNOLOGY", "R0003MA", NA, 6
#' )
#'
#'
#' derive_vars_crit(
#' dataset = input,
#' prefix = "CRIT1",
#' crit_label = "Titer >= ISLLOQ",
#' condition = !is.na(AVAL) & !is.na(ISLLOQ),
#' criterion = AVAL >= ISLLOQ
#' )
#'
derive_vars_crit <- function(dataset, prefix, crit_label, condition, criterion) {
condition <- assert_filter_cond(enquo(condition))
criterion <- assert_filter_cond(enquo(criterion))
var_char <- paste0(prefix, "FL")
var_num <- paste0(prefix, "FN")
if (grepl("CRIT", prefix)) {
data <- dataset %>%
mutate(
`:=`(
!!var_char,
case_when(
!(!!condition) ~ NA_character_,
!!criterion & !!condition ~ "Y",
!(!!criterion) & !!condition ~ "N"
)
),
`:=`(
!!var_num,
case_when(
!(!!condition) ~ NA_real_,
!!criterion & !!condition ~ 1,
!(!!criterion) & !!condition ~ 0
)
),
`:=`(
!!prefix,
case_when(
!(!!condition) ~ NA_character_,
!!criterion & !!condition ~ crit_label,
!(!!criterion) & !!condition ~ crit_label
)
)
)
} else {
data <- dataset %>%
mutate(
`:=`(
!!var_char,
case_when(
!(!!condition) ~ NA_character_,
!!criterion & !!condition ~ "Y",
!(!!criterion) & !!condition ~ "N"
)
)
)
}
return(data)
}