Skip to contents

[Stable]

Functions for analyzing frequencies and fractions of occurrences for patients with occurrence data. Primary analysis variables are the dictionary terms. All occurrences are counted for total counts. Multiple occurrences within patient at the lowest term level displayed in the table are counted only once.

Usage

s_count_occurrences(
  df,
  denom = c("N_col", "n"),
  .N_col,
  .df_row,
  drop = TRUE,
  .var = "MHDECOD",
  id = "USUBJID"
)

a_count_occurrences(
  df,
  denom = c("N_col", "n"),
  .N_col,
  .df_row,
  drop = TRUE,
  .var = "MHDECOD",
  id = "USUBJID"
)

count_occurrences(
  lyt,
  vars,
  var_labels = vars,
  show_labels = "hidden",
  ...,
  table_names = vars,
  .stats = "count_fraction",
  .formats = NULL,
  .labels = NULL,
  .indent_mods = NULL
)

Arguments

df

(data.frame)
data set containing all analysis variables.

denom

(string)
choice of denominator for patient proportions. Can be:

  • N_col: total number of patients in this column across rows

  • n: number of patients with any occurrences

.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.

.df_row

(data.frame)
data frame across all of the columns for the given row split.

drop

(flag)
should non appearing occurrence levels be dropped from the resulting table. Note that in that case the remaining occurrence levels in the table are sorted alphabetically.

.var

(string)
single variable name that is passed by rtables when requested by a statistics function.

id

(string)
subject variable name.

lyt

(layout)
input layout where analyses will be added to.

vars

(character)
variable names for the primary analysis variable to be iterated over.

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 vars are analyzed multiple times, to avoid warnings from rtables.

.stats

(character)
statistics to select for the table.

.formats

(named character or list)
formats for the statistics.

.labels

(named character)
labels for the statistics (without indent).

.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_occurrences() returns a list with:

    • count: list of counts with one element per occurrence.

    • count_fraction: list of counts and fractions with one element per occurrence.

    • fraction: list of numerators and denominators with one element per occurrence.

  • count_occurrences() returns a layout object suitable for passing to further layouting functions, or to rtables::build_table(). Adding this function to an rtable layout will add formatted rows containing the statistics from s_count_occurrences() to the table layout.

Functions

  • s_count_occurrences(): Statistics function which counts number of patients that report an occurrence.

  • a_count_occurrences(): Formatted analysis function which is used as afun in count_occurrences().

  • count_occurrences(): Layout-creating function which can take statistics function arguments and additional format arguments. This function is a wrapper for rtables::analyze().

Note

By default, occurrences which don't appear in a given row split are dropped from the table and the occurrences in the table are sorted alphabetically per row split. Therefore, the corresponding layout needs to use split_fun = drop_split_levels in the split_rows_by calls. Use drop = FALSE if you would like to show all occurrences.

Examples

df <- data.frame(
  USUBJID = as.character(c(1, 1, 2, 4, 4, 4)),
  MHDECOD = c("MH1", "MH2", "MH1", "MH1", "MH1", "MH3")
)

N_per_col <- 4L

# Count unique occurrences per subject.
s_count_occurrences(
  df,
  .N_col = N_per_col,
  .df_row = df,
  .var = "MHDECOD",
  id = "USUBJID"
)
#> $count
#> $count$MH1
#> [1] 3
#> 
#> $count$MH2
#> [1] 1
#> 
#> $count$MH3
#> [1] 1
#> 
#> 
#> $count_fraction
#> $count_fraction$MH1
#> [1] 3.00 0.75
#> 
#> $count_fraction$MH2
#> [1] 1.00 0.25
#> 
#> $count_fraction$MH3
#> [1] 1.00 0.25
#> 
#> 
#> $fraction
#> $fraction$MH1
#>   num denom 
#>     3     4 
#> 
#> $fraction$MH2
#>   num denom 
#>     1     4 
#> 
#> $fraction$MH3
#>   num denom 
#>     1     4 
#> 
#> 

#  We need to ungroup `count_fraction` first so that the `rtables` formatting
# function `format_count_fraction()` can be applied correctly.
afun <- make_afun(a_count_occurrences, .ungroup_stats = c("count", "count_fraction", "fraction"))
afun(
  df,
  .N_col = N_per_col,
  .df_row = df,
  .var = "MHDECOD",
  id = "USUBJID"
)
#> RowsVerticalSection (in_rows) object print method:
#> ----------------------------
#>   row_name formatted_cell indent_mod row_label
#> 1      MH1              3          0       MH1
#> 2      MH2              1          0       MH2
#> 3      MH3              1          0       MH3
#> 4      MH1      3 (75.0%)          0       MH1
#> 5      MH2      1 (25.0%)          0       MH2
#> 6      MH3      1 (25.0%)          0       MH3
#> 7      MH1    3/4 (75.0%)          0       MH1
#> 8      MH2    1/4 (25.0%)          0       MH2
#> 9      MH3    1/4 (25.0%)          0       MH3

library(dplyr)
df <- data.frame(
  USUBJID = as.character(c(
    1, 1, 2, 4, 4, 4,
    6, 6, 6, 7, 7, 8
  )),
  MHDECOD = c(
    "MH1", "MH2", "MH1", "MH1", "MH1", "MH3",
    "MH2", "MH2", "MH3", "MH1", "MH2", "MH4"
  ),
  ARM = rep(c("A", "B"), each = 6)
)
df_adsl <- df %>%
  select(USUBJID, ARM) %>%
  unique()

# Create table layout
lyt <- basic_table() %>%
  split_cols_by("ARM") %>%
  add_colcounts() %>%
  count_occurrences(vars = "MHDECOD", .stats = c("count_fraction"))

# Apply table layout to data and produce `rtable` object
lyt %>%
  build_table(df, alt_counts_df = df_adsl) %>%
  prune_table()
#>           A           B    
#>         (N=3)       (N=3)  
#> ———————————————————————————
#> MH1   3 (100%)    1 (33.3%)
#> MH2   1 (33.3%)   2 (66.7%)
#> MH3   1 (33.3%)   1 (33.3%)
#> MH4       0       1 (33.3%)