library(dplyr)
# Add labelled flag variables to analysis dataset.
adae <- tern_ex_adae %>%
mutate(
fl1 = TRUE %>% with_label("Total AEs"),
fl2 = (TRTEMFL == "Y") %>%
with_label("Total number of patients with at least one adverse event"),
fl3 = (TRTEMFL == "Y" & AEOUT == "FATAL") %>%
with_label("Total number of patients with fatal AEs"),
fl4 = (TRTEMFL == "Y" & AEOUT == "FATAL" & AEREL == "Y") %>%
with_label("Total number of patients with related fatal AEs")
)
# `count_patients_with_flags()`
lyt2 <- basic_table() %>%
split_cols_by("ARM") %>%
add_colcounts() %>%
count_patients_with_flags(
"SUBJID",
flag_variables = c("fl1", "fl2", "fl3", "fl4"),
denom = "N_col"
)
build_table(lyt2, adae, alt_counts_df = tern_ex_adsl)
# `s_count_patients_with_flags()`
s_count_patients_with_flags(
adae,
"SUBJID",
flag_variables = c("fl1", "fl2", "fl3", "fl4"),
denom = "N_col",
.N_col = 1000
)
# We need to ungroup `count_fraction` first so that the `rtables` formatting
# function `format_count_fraction()` can be applied correctly.
# `a_count_patients_with_flags()`
afun <- make_afun(a_count_patients_with_flags,
.stats = "count_fraction",
.ungroup_stats = "count_fraction"
)
afun(
adae,
.N_col = 10L,
.N_row = 10L,
.var = "USUBJID",
flag_variables = c("fl1", "fl2", "fl3", "fl4")
)
Run the code above in your browser using DataLab