Count the Number of Patients with Particular Flags
Source:R/count_patients_with_flags.R
count_patients_with_flags.Rd
Usage
s_count_patients_with_flags(
df,
.var,
flag_variables,
flag_labels = NULL,
.N_col,
.N_row,
denom = c("n", "N_row", "N_col")
)
a_count_patients_with_flags(
df,
.var,
flag_variables,
flag_labels = NULL,
.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.- flag_variables
(
character
)
a character vector specifying the names oflogical
variables from analysis dataset used for counting the number of unique identifiers.- flag_labels
(
character
)
vector of labels to use for flag variables.- .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. Options are:n
: number of values in this row and column intersection.N_row
: total number of values in this row across columns.N_col
: total number of values in this column across rows.
- lyt
(
layout
)
input layout where analyses will be added to.- var
(
string
)
single variable name that is passed byrtables
when requested by a statistics function.- var_labels
(
character
)
character for label.- show_labels
(
string
)
label visibility: one of "default", "visible" and "hidden".- ...
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.- .indent_mods
(named
integer
)
indent modifiers for the labels. Defaults to 0, which corresponds to the unmodified default behavior. Can be negative.
Value
s_count_patients_with_flags()
returns the count and the fraction of unique identifiers with each particular flag as a list of statisticsn
,count
,count_fraction
, andn_blq
, with one element per flag.
a_count_patients_with_flags()
returns the corresponding list with formattedrtables::CellValue()
.
count_patients_with_flags()
returns a layout object suitable for passing to further layouting functions, or tortables::build_table()
. Adding this function to anrtable
layout will add formatted rows containing the statistics froms_count_patients_with_flags()
to the table layout.
Functions
s_count_patients_with_flags()
: Statistics function which counts the number of patients for which a particular flag variable isTRUE
.a_count_patients_with_flags()
: Formatted analysis function which is used asafun
incount_patients_with_flags()
.count_patients_with_flags()
: Layout-creating function which can take statistics function arguments and additional format arguments. This function is a wrapper forrtables::analyze()
.
Note
If flag_labels
is not specified, variables labels will be extracted from df
. If variables are not
labeled, variable names will be used instead. Alternatively, a named vector
can be supplied to
flag_variables
such that within each name-value pair the name corresponds to the variable name and the value is
the label to use for this variable.
Examples
library(dplyr)
# `s_count_patients_with_flags()`
# Add labelled flag variables to analysis dataset.
adae <- tern_ex_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$`Total AEs`
#> [1] 164
#>
#> $n$`Total number of patients with at least one adverse event`
#> [1] 164
#>
#> $n$`Total number of patients with fatal AEs`
#> [1] 164
#>
#> $n$`Total number of patients with related fatal AEs`
#> [1] 164
#>
#>
#> $count
#> $count$`Total AEs`
#> [1] 164
#>
#> $count$`Total number of patients with at least one adverse event`
#> [1] 164
#>
#> $count$`Total number of patients with fatal AEs`
#> [1] 79
#>
#> $count$`Total number of patients with related fatal AEs`
#> [1] 79
#>
#>
#> $count_fraction
#> $count_fraction$`Total AEs`
#> [1] 164.000 0.164
#>
#> $count_fraction$`Total number of patients with at least one adverse event`
#> [1] 164.000 0.164
#>
#> $count_fraction$`Total number of patients with fatal AEs`
#> [1] 79.000 0.079
#>
#> $count_fraction$`Total number of patients with related fatal AEs`
#> [1] 79.000 0.079
#>
#>
#> $n_blq
#> $n_blq$`Total AEs`
#> [1] 0
#>
#> $n_blq$`Total number of patients with at least one adverse event`
#> [1] 0
#>
#> $n_blq$`Total number of patients with fatal AEs`
#> [1] 0
#>
#> $n_blq$`Total number of patients with related fatal AEs`
#> [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,
.N_row = 10L,
.var = "USUBJID",
flag_variables = c("fl1", "fl2", "fl3", "fl4")
)
#> RowsVerticalSection (in_rows) object print method:
#> ----------------------------
#> row_name formatted_cell
#> 1 Total AEs 164 (100%)
#> 2 Total number of patients with at least one adverse event 164 (100%)
#> 3 Total number of patients with fatal AEs 79 (48.2%)
#> 4 Total number of patients with related fatal AEs 79 (48.2%)
#> indent_mod row_label
#> 1 0 Total AEs
#> 2 0 Total number of patients with at least one adverse event
#> 3 0 Total number of patients with fatal AEs
#> 4 0 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)
#> A: Drug X B: Placebo C: Combination
#> (N=69) (N=73) (N=58)
#> ———————————————————————————————————————————————————————————————————————————————————————————————————
#> Total AEs 59 (85.5%) 57 (78.1%) 48 (82.8%)
#> Total number of patients with at least one adverse event 59 (85.5%) 57 (78.1%) 48 (82.8%)
#> Total number of patients with fatal AEs 28 (40.6%) 31 (42.5%) 20 (34.5%)
#> Total number of patients with related fatal AEs 28 (40.6%) 31 (42.5%) 20 (34.5%)