Helper Function to create a new SMQ
variable in ADAE
by stacking
SMQ
and/or CQ
records.
Source: R/h_stack_by_baskets.R
h_stack_by_baskets.Rd
Helper Function to create a new SMQ
variable in ADAE
that
consists of all adverse events belonging to selected
Standardized/Customized queries.
The new dataset will only contain records of the adverse events
belonging to any of the selected baskets.
Arguments
- df
(
data.frame
)
data set containing all analysis variables.- baskets
(
character
)
variable names of the selected Standardized/Customized queries.- smq_varlabel
(
string
)
a label for the new variable created.- keys
(
character
)
names of the key variables to be returned along with the new variable created.- aag_summary
(
data.frame
)
containing theSMQ
baskets and the levels of interest for the finalSMQ
variable. This is useful when there are some levels of interest that are not observed in thedf
dataset. The two columns of this dataset should be namedbasket
andbasket_name
.- na_level
(
string
)
used to replace allNA
or empty values in factors with customstring
.
Examples
adae <- tern_ex_adae[1:20, ] %>% df_explicit_na()
h_stack_by_baskets(df = adae)
#> # A tibble: 8 × 6
#> STUDYID USUBJID ASTDTM AEDECOD AESEQ SMQ
#> <fct> <fct> <dttm> <fct> <int> <fct>
#> 1 AB12345 AB12345-BRA-11-id-8 2021-12-05 00:00:00 dcd D.2.1.5.3 2 D.2.1.5…
#> 2 AB12345 AB12345-BRA-12-id-120 2020-02-05 00:00:00 dcd D.2.1.5.3 2 D.2.1.5…
#> 3 AB12345 AB12345-BRA-1-id-171 2022-11-29 00:00:00 dcd C.1.1.1.3 2 C.1.1.1…
#> 4 AB12345 AB12345-BRA-1-id-23 2020-07-10 00:00:00 dcd B.2.2.3.1 3 C.1.1.1…
#> 5 AB12345 AB12345-BRA-1-id-59 2021-10-11 00:00:00 dcd C.1.1.1.3 4 C.1.1.1…
#> 6 AB12345 AB12345-BRA-1-id-9 2021-06-01 00:00:00 dcd C.1.1.1.3 1 C.1.1.1…
#> 7 AB12345 AB12345-BRA-11-id-8 2021-12-21 00:00:00 dcd C.1.1.1.3 3 C.1.1.1…
#> 8 AB12345 AB12345-BRA-12-id-120 2020-10-01 00:00:00 dcd C.1.1.1.3 3 C.1.1.1…
aag <- data.frame(
NAMVAR = c("CQ01NAM", "CQ02NAM", "SMQ01NAM", "SMQ02NAM"),
REFNAME = c(
"D.2.1.5.3/A.1.1.1.1 AESI", "X.9.9.9.9/Y.8.8.8.8 AESI",
"C.1.1.1.3/B.2.2.3.1 AESI", "C.1.1.1.3/B.3.3.3.3 AESI"
),
SCOPE = c("", "", "BROAD", "BROAD"),
stringsAsFactors = FALSE
)
basket_name <- character(nrow(aag))
cq_pos <- grep("^(CQ).+NAM$", aag$NAMVAR)
smq_pos <- grep("^(SMQ).+NAM$", aag$NAMVAR)
basket_name[cq_pos] <- aag$REFNAME[cq_pos]
basket_name[smq_pos] <- paste0(
aag$REFNAME[smq_pos], "(", aag$SCOPE[smq_pos], ")"
)
aag_summary <- data.frame(
basket = aag$NAMVAR,
basket_name = basket_name,
stringsAsFactors = TRUE
)
result <- h_stack_by_baskets(df = adae, aag_summary = aag_summary)
#> Warning: There are 0 basket levels in common between aag_summary$basket_name and df.
all(levels(aag_summary$basket_name) %in% levels(result$SMQ))
#> [1] TRUE
h_stack_by_baskets(
df = adae,
aag_summary = NULL,
keys = c("STUDYID", "USUBJID", "AEDECOD", "ARM"),
baskets = "SMQ01NAM"
)
#> # A tibble: 6 × 5
#> STUDYID USUBJID AEDECOD ARM SMQ
#> <fct> <fct> <fct> <fct> <fct>
#> 1 AB12345 AB12345-BRA-1-id-171 dcd C.1.1.1.3 B: Placebo C.1.1.1.3/B.2.2.3.…
#> 2 AB12345 AB12345-BRA-1-id-23 dcd B.2.2.3.1 A: Drug X C.1.1.1.3/B.2.2.3.…
#> 3 AB12345 AB12345-BRA-1-id-59 dcd C.1.1.1.3 A: Drug X C.1.1.1.3/B.2.2.3.…
#> 4 AB12345 AB12345-BRA-1-id-9 dcd C.1.1.1.3 C: Combination C.1.1.1.3/B.2.2.3.…
#> 5 AB12345 AB12345-BRA-11-id-8 dcd C.1.1.1.3 A: Drug X C.1.1.1.3/B.2.2.3.…
#> 6 AB12345 AB12345-BRA-12-id-120 dcd C.1.1.1.3 A: Drug X C.1.1.1.3/B.2.2.3.…