Count the Number of Patients with Particular Flags
Source:R/count_patients_with_flags.R
      count_patients_with_flags.RdUsage
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.
- flag_variables
- ( - character)
 a character vector specifying the names of- logicalvariables from analysis dataset used for counting the number of unique identifiers.
- .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 by- rtables.
- .N_row
- ( - count)
 column-wise N (column count) for the full column that is passed by- rtables.
- 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 by- rtableswhen 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 same- varsare analyzed multiple times, to avoid warnings from- rtables.
- .stats
- ( - character)
 statistics to select for the table.
- .formats
- (named - characteror- list)
 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 statistics- n,- count,- count_fraction, and- n_blq, with one element per flag.
- a_count_patients_with_flags()returns the corresponding list with formatted- rtables::CellValue().
- count_patients_with_flags()returns a layout object suitable for passing to further layouting functions, or to- rtables::build_table(). Adding this function to an- rtablelayout will add formatted rows containing the statistics from- s_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 is- TRUE.
- a_count_patients_with_flags(): Formatted analysis function which is used as- afunin- count_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 for- rtables::analyze().
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$fl1
#> [1] 164
#> 
#> $n$fl2
#> [1] 164
#> 
#> $n$fl3
#> [1] 164
#> 
#> $n$fl4
#> [1] 164
#> 
#> 
#> $count
#> $count$fl1
#> [1] 164
#> 
#> $count$fl2
#> [1] 164
#> 
#> $count$fl3
#> [1] 79
#> 
#> $count$fl4
#> [1] 79
#> 
#> 
#> $count_fraction
#> $count_fraction$fl1
#> [1] 164.000   0.164
#> 
#> $count_fraction$fl2
#> [1] 164.000   0.164
#> 
#> $count_fraction$fl3
#> [1] 79.000  0.079
#> 
#> $count_fraction$fl4
#> [1] 79.000  0.079
#> 
#> 
#> $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,
  .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     164 (100%)          0       fl1
#> 2      fl2     164 (100%)          0       fl2
#> 3      fl3     79 (48.2%)          0       fl3
#> 4      fl4     79 (48.2%)          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 = 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%)