Count the Number of Patients with a Particular Event
Source:R/count_patients_with_event.R
count_patients_with_event.Rd
Usage
s_count_patients_with_event(
df,
.var,
filters,
.N_col,
.N_row,
denom = c("n", "N_row", "N_col")
)
a_count_patients_with_event(
df,
.var,
filters,
.N_col,
.N_row,
denom = c("n", "N_row", "N_col")
)
count_patients_with_event(
lyt,
vars,
...,
table_names = vars,
.stats = "count_fraction",
.formats = NULL,
.labels = NULL,
.indent_mods = NULL
)
s_count_patients_with_flags(
df,
.var,
flag_variables,
.N_col,
.N_row,
denom = c("n", "N_row", "N_col")
)
a_count_patients_with_flags(
df,
.var,
flag_variables,
.N_col,
.N_row,
denom = c("n", "N_row", "N_col")
)
count_patients_with_flags(
lyt,
var,
var_labels = var,
show_labels = "hidden",
...,
table_names = paste0("tbl_flags_", var),
.stats = "count_fraction",
.formats = NULL,
.indent_mods = NULL
)
Arguments
- df
(
data frame
)
data set containing all analysis variables.- .var
(
character
)
name of the column that contains the unique identifier.- filters
(
character
)
a character vector specifying the column names and flag variables to be used for counting the number of unique identifiers satisfying such conditions. Multiple column names and flags are accepted in this formatc("column_name1" = "flag1", "column_name2" = "flag2")
. Note that only equality is being accepted as condition.- .N_col
(
count
)
row-wise N (row group count) for the group of observations being analyzed (i.e. with no column-based subsetting) that is passed byrtables
.- .N_row
(
count
)
column-wise N (column count) for the full column that is passed byrtables
.- denom
(
string
)
choice of denominator for proportion:
can ben
(number of values in this row and column intersection),N_row
(total number of values in this row across columns), orN_col
(total number of values in this column across rows).- lyt
(
layout
)
input layout where analyses will be added to.- vars
(
character
)
variable names for the primary analysis variable to be iterated over.- ...
additional arguments for the lower level functions.
- table_names
(
character
)
this can be customized in case that the samevars
are analyzed multiple times, to avoid warnings fromrtables
.- .stats
(
character
)
statistics to select for the table.- .formats
(named
character
orlist
)
formats for the statistics.- .labels
(named
character
)
labels for the statistics (without indent).- .indent_mods
(named
integer
)
indent modifiers for the labels.- flag_variables
(
character
)
a character vector specifying the names oflogical
variables from analysis dataset used for counting the number of unique identifiers.- var
(
string
)
single variable name that is passed byrtables
when requested by a statistics function.- var_labels
character for label.
- show_labels
label visibility: one of "default", "visible" and "hidden".
Value
s_count_patients_with_event()
returns the count and fraction of patients with the
defined event.
Functions
s_count_patients_with_event()
: Statistics Function that returns the number and the fraction of unique identifiers with a particular type of event, e.g. the number and the fraction of patients who had treatment-emergent adverse events. Note that the user can define a new data column containing the event of interest.a_count_patients_with_event()
: Formatted Analysis function which can be further customized by callingrtables::make_afun()
on it. It is used asafun
inrtables::analyze()
.count_patients_with_event()
: Analyze Function which adds the count statistics to the input layout. Note that additional formatting arguments can be used here.s_count_patients_with_flags()
: Statistics function that returns the number and the fraction of unique identifiers with each particular flag. Returns a list of totals, counts, counts and fractions with one element per flag.a_count_patients_with_flags()
: Formatted Analysis function which can be further customized by callingrtables::make_afun()
on it. It is used asafun
inrtables::analyze()
.count_patients_with_flags()
: Analyze Function which is a modified version ofcount_patients_with_event()
. Adds the count statistics to the input layout for multiple flag variables at once.
Examples
library(dplyr)
library(scda)
adae <- synthetic_cdisc_data("latest")$adae
adsl <- synthetic_cdisc_data("latest")$adsl
# `s_count_patients_with_event()`
s_count_patients_with_event(
adae,
.var = "SUBJID",
filters = c("TRTEMFL" = "Y")
)
#> $n
#> [1] 365
#>
#> $count
#> [1] 365
#>
#> $count_fraction
#> [1] 365 1
#>
#> $n_blq
#> [1] 0
#>
s_count_patients_with_event(
adae,
.var = "SUBJID",
filters = c("TRTEMFL" = "Y", "AEOUT" = "FATAL")
)
#> $n
#> [1] 365
#>
#> $count
#> [1] 221
#>
#> $count_fraction
#> [1] 221.0000000 0.6054795
#>
#> $n_blq
#> [1] 0
#>
s_count_patients_with_event(
adae,
.var = "SUBJID",
filters = c("TRTEMFL" = "Y", "AEOUT" = "FATAL"),
denom = "N_col",
.N_col = 456
)
#> $n
#> [1] 365
#>
#> $count
#> [1] 221
#>
#> $count_fraction
#> [1] 221.0000000 0.4846491
#>
#> $n_blq
#> [1] 0
#>
# `a_count_patients_with_event()`
a_count_patients_with_event(
adae,
.var = "SUBJID",
filters = c("TRTEMFL" = "Y"),
.N_col = 100,
.N_row = 100
)
#> RowsVerticalSection (in_rows) object print method:
#> ----------------------------
#> row_name formatted_cell indent_mod row_label
#> 1 n 365 0 n
#> 2 count 365 0 count
#> 3 count_fraction 365 (100%) 0 count_fraction
#> 4 n_blq 0 0 n_blq
# `count_patients_with_event()`
lyt <- basic_table() %>%
split_cols_by("ARM") %>%
add_colcounts() %>%
count_values(
"STUDYID",
values = "AB12345",
.stats = "count",
.labels = c(count = "Total AEs")
) %>%
count_patients_with_event(
"SUBJID",
filters = c("TRTEMFL" = "Y"),
.labels = c(count_fraction = "Total number of patients with at least one adverse event"),
table_names = "tbl_all"
) %>%
count_patients_with_event(
"SUBJID",
filters = c("TRTEMFL" = "Y", "AEOUT" = "FATAL"),
.labels = c(count_fraction = "Total number of patients with fatal AEs"),
table_names = "tbl_fatal"
) %>%
count_patients_with_event(
"SUBJID",
filters = c("TRTEMFL" = "Y", "AEOUT" = "FATAL", "AEREL" = "Y"),
.labels = c(count_fraction = "Total number of patients with related fatal AEs"),
.indent_mods = c(count_fraction = 2L),
table_names = "tbl_rel_fatal"
)
build_table(lyt, adae, alt_counts_df = adsl)
#> A: Drug X B: Placebo C: Combination
#> (N=134) (N=134) (N=132)
#> ———————————————————————————————————————————————————————————————————————————————————————————————————
#> Total AEs 609 622 703
#> Total number of patients with at least one adverse event 122 (100%) 123 (100%) 120 (100%)
#> Total number of patients with fatal AEs 76 (62.3%) 70 (56.9%) 75 (62.5%)
#> Total number of patients with related fatal AEs 76 (62.3%) 70 (56.9%) 75 (62.5%)
# `s_count_patients_with_flags()`
# Add labelled flag variables to analysis dataset.
adae <- adae %>%
mutate(
fl1 = TRUE,
fl2 = TRTEMFL == "Y",
fl3 = TRTEMFL == "Y" & AEOUT == "FATAL",
fl4 = TRTEMFL == "Y" & AEOUT == "FATAL" & AEREL == "Y"
)
labels <- c(
"fl1" = "Total AEs",
"fl2" = "Total number of patients with at least one adverse event",
"fl3" = "Total number of patients with fatal AEs",
"fl4" = "Total number of patients with related fatal AEs"
)
formatters::var_labels(adae)[names(labels)] <- labels
s_count_patients_with_flags(
adae,
"SUBJID",
flag_variables = c("fl1", "fl2", "fl3", "fl4"),
denom = "N_col",
.N_col = 1000
)
#> $n
#> $n$fl1
#> [1] 365
#>
#> $n$fl2
#> [1] 365
#>
#> $n$fl3
#> [1] 365
#>
#> $n$fl4
#> [1] 365
#>
#>
#> $count
#> $count$fl1
#> [1] 365
#>
#> $count$fl2
#> [1] 365
#>
#> $count$fl3
#> [1] 221
#>
#> $count$fl4
#> [1] 221
#>
#>
#> $count_fraction
#> $count_fraction$fl1
#> [1] 365.000 0.365
#>
#> $count_fraction$fl2
#> [1] 365.000 0.365
#>
#> $count_fraction$fl3
#> [1] 221.000 0.221
#>
#> $count_fraction$fl4
#> [1] 221.000 0.221
#>
#>
#> $n_blq
#> $n_blq$fl1
#> [1] 0
#>
#> $n_blq$fl2
#> [1] 0
#>
#> $n_blq$fl3
#> [1] 0
#>
#> $n_blq$fl4
#> [1] 0
#>
#>
# 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, # nolint
.N_row = 10L,
.var = "USUBJID",
flag_variables = c("fl1", "fl2", "fl3", "fl4")
)
#> RowsVerticalSection (in_rows) object print method:
#> ----------------------------
#> row_name formatted_cell indent_mod row_label
#> 1 fl1 365 (100%) 0 fl1
#> 2 fl2 365 (100%) 0 fl2
#> 3 fl3 221 (60.5%) 0 fl3
#> 4 fl4 221 (60.5%) 0 fl4
# `count_patients_with_flags()`
lyt2 <- basic_table() %>%
split_cols_by("ARM") %>%
add_colcounts() %>%
count_patients_with_flags(
"SUBJID",
flag_variables = formatters::var_labels(adae[, c("fl1", "fl2", "fl3", "fl4")]),
denom = "N_col"
)
build_table(lyt2, adae, alt_counts_df = adsl)
#> A: Drug X B: Placebo C: Combination
#> (N=134) (N=134) (N=132)
#> ————————————————————————————————————————————————————————————————————————————————————————————————————
#> Total AEs 122 (91%) 123 (91.8%) 120 (90.9%)
#> Total number of patients with at least one adverse event 122 (91%) 123 (91.8%) 120 (90.9%)
#> Total number of patients with fatal AEs 76 (56.7%) 70 (52.2%) 75 (56.8%)
#> Total number of patients with related fatal AEs 76 (56.7%) 70 (52.2%) 75 (56.8%)