Skip to contents

Compute Analysis Results Data (ARD) for categorical summary statistics.

Usage

ard_categorical(
  data,
  variables,
  by = dplyr::group_vars(data),
  strata = NULL,
  statistic = everything() ~ categorical_summary_fns(),
  denominator = NULL,
  fmt_fn = NULL,
  stat_label = everything() ~ default_stat_labels()
)

Arguments

data

(data.frame)
a data frame

variables

(tidy-select)
columns to include in summaries. Default is everything().

by, strata

(tidy-select)
columns to tabulate by/stratify by for tabulation. Arguments are similar, but with an important distinction:

by: results are tabulated by all combinations of the columns specified, including unobserved combinations and unobserved factor levels.

strata: results are tabulated by all observed combinations of the columns specified.

Arguments may be used in conjunction with one another.

statistic

(formula-list-selector)
a named list, a list of formulas, or a single formula where the list element is a named list of functions (or the RHS of a formula), e.g. list(mpg = categorical_summary_fns()).

denominator

(data.frame, integer)
Specify this optional argument to change the denominator, e.g. the "N" statistic. Default is NULL. See below for details.

fmt_fn

(formula-list-selector)
a named list, a list of formulas, or a single formula where the list element is a named list of functions (or the RHS of a formula), e.g. list(mpg = list(mean = \(x) round(x, digits = 2) |> as.character)).

stat_label

(formula-list-selector)
a named list, a list of formulas, or a single formula where the list element is either a named list or a list of formulas defining the statistic labels, e.g. everything() ~ list(n = "n", p = "pct") or everything() ~ list(n ~ "n", p ~ "pct").

Value

an ARD data frame of class 'card'

Denominators

By default, the ard_categorical() function returns the statistics "n", "N", and "p", where little "n" are the counts for the variable levels, and big "N" is the number of non-missing observations. The default calculation for the percentage is merely p = n/N.

However, it is sometimes necessary to provide a different "N" to use as the denominator in this calculation. For example, in a calculation of the rates of various observed adverse events, you may need to update the denominator to the number of enrolled subjects.

In such cases, use the denominator argument to specify a new definition of "N", and subsequently "p". The argument expects one of the following inputs:

  • a data frame. Any columns in the data frame that overlap with the by/strata columns will be used to calculate the new "N".

  • an integer. This single integer will be used as the new "N"

  • a string: one of "column", "row", or "cell". "column" is equivalent to denominator=NULL. "row" gives 'row' percentages where by/strata columns are the 'top' of a cross table, and the variables are the rows. "cell" gives percentages where the denominator is the number of non-missing rows in the source data frame.

  • a structured data frame. The data frame will include columns from by/strata. The last column must be named "...ard_N...". The integers in this column will be used as the updated "N" in the calculations.

Examples

ard_categorical(ADSL, by = "ARM", variables = "AGEGR1")
#> {cards} data frame: 27 x 11
#>    group1 group1_level variable variable_level stat_name stat_label  stat
#> 1     ARM      Placebo   AGEGR1          65-80         n          n    42
#> 2     ARM      Placebo   AGEGR1          65-80         N          N    86
#> 3     ARM      Placebo   AGEGR1          65-80         p          % 0.488
#> 4     ARM    Xanomeli…   AGEGR1          65-80         n          n    55
#> 5     ARM    Xanomeli…   AGEGR1          65-80         N          N    84
#> 6     ARM    Xanomeli…   AGEGR1          65-80         p          % 0.655
#> 7     ARM    Xanomeli…   AGEGR1          65-80         n          n    47
#> 8     ARM    Xanomeli…   AGEGR1          65-80         N          N    84
#> 9     ARM    Xanomeli…   AGEGR1          65-80         p          %  0.56
#> 10    ARM      Placebo   AGEGR1            <65         n          n    14
#>  17 more rows
#>  Use `print(n = ...)` to see more rows
#>  4 more variables: context, fmt_fn, warning, error

ADSL |>
  dplyr::group_by(ARM) |>
  ard_categorical(
    variables = "AGEGR1",
    statistic = everything() ~ categorical_summary_fns("n")
  )
#> {cards} data frame: 9 x 11
#>   group1 group1_level variable variable_level stat_name stat_label stat
#> 1    ARM      Placebo   AGEGR1          65-80         n          n   42
#> 2    ARM    Xanomeli…   AGEGR1          65-80         n          n   55
#> 3    ARM    Xanomeli…   AGEGR1          65-80         n          n   47
#> 4    ARM      Placebo   AGEGR1            <65         n          n   14
#> 5    ARM    Xanomeli…   AGEGR1            <65         n          n   11
#> 6    ARM    Xanomeli…   AGEGR1            <65         n          n    8
#> 7    ARM      Placebo   AGEGR1            >80         n          n   30
#> 8    ARM    Xanomeli…   AGEGR1            >80         n          n   18
#> 9    ARM    Xanomeli…   AGEGR1            >80         n          n   29
#>  4 more variables: context, fmt_fn, warning, error