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.
Examples
library(scda)
adae <- synthetic_cdisc_data("latest")$adae[1:20, ] %>% df_explicit_na()
h_stack_by_baskets(df = adae)
#> # A tibble: 4 × 6
#> STUDYID USUBJID ASTDTM AEDECOD AESEQ SMQ
#> <fct> <fct> <dttm> <fct> <int> <fct>
#> 1 AB12345 AB12345-BRA-1-id-141 2021-10-23 00:00:00 dcd D.2.1.5.3 2 D.2.1.5.…
#> 2 AB12345 AB12345-BRA-1-id-141 2022-08-03 00:00:00 dcd A.1.1.1.1 3 D.2.1.5.…
#> 3 AB12345 AB12345-BRA-1-id-141 2023-03-04 00:00:00 dcd A.1.1.1.1 5 D.2.1.5.…
#> 4 AB12345 AB12345-BRA-1-id-265 2021-09-12 00:00:00 dcd C.1.1.1.3 4 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)
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: 1 × 5
#> STUDYID USUBJID AEDECOD ARM SMQ
#> <fct> <fct> <fct> <fct> <fct>
#> 1 AB12345 AB12345-BRA-1-id-265 dcd C.1.1.1.3 C: Combination C.1.1.1.3/B.2.2.3.1…