Skip to contents

Use this function to add a new statistic row that is a function of the other statistics in an ARD.

Usage

add_calculated_row(
  x,
  expr,
  stat_name,
  by = c(all_ard_groups(), all_ard_variables(), any_of("context")),
  stat_label = stat_name,
  fmt_fn = NULL
)

Arguments

x

(card)
data frame of class 'card'

expr

(expression)
an expression

stat_name

(string)
string naming the new statistic

by

(tidy-select)
Grouping variables to calculate statistics within

stat_label

(string)
string of the statistic label. Default is the stat_name.

fmt_fn

(integer, function, string)
a function of an integer or string that can be converted to a function with alias_as_fmt_fn().

Value

an ARD data frame of class 'card'

Examples

ard_continuous(mtcars, variables = mpg) |>
  add_calculated_row(expr = max - min, stat_name = "range")
#> {cards} data frame: 9 x 8
#>   variable   context stat_name stat_label   stat fmt_fn
#> 1      mpg continuo…         N          N     32      0
#> 2      mpg continuo…      mean       Mean 20.091      1
#> 3      mpg continuo…        sd         SD  6.027      1
#> 4      mpg continuo…    median     Median   19.2      1
#> 5      mpg continuo…       p25         Q1  15.35      1
#> 6      mpg continuo…       p75         Q3   22.8      1
#> 7      mpg continuo…       min        Min   10.4      1
#> 8      mpg continuo…       max        Max   33.9      1
#> 9      mpg continuo…     range      range   23.5      1
#>  2 more variables: warning, error

ard_continuous(mtcars, variables = mpg) |>
  add_calculated_row(
    expr =
      dplyr::case_when(
        mean > median ~ "Right Skew",
        mean < median ~ "Left Skew",
        .default = "Symmetric"
      ),
    stat_name = "skew"
  )
#> {cards} data frame: 9 x 8
#>   variable   context stat_name stat_label      stat fmt_fn
#> 1      mpg continuo…         N          N        32      0
#> 2      mpg continuo…      mean       Mean    20.091      1
#> 3      mpg continuo…        sd         SD     6.027      1
#> 4      mpg continuo…    median     Median      19.2      1
#> 5      mpg continuo…       p25         Q1     15.35      1
#> 6      mpg continuo…       p75         Q3      22.8      1
#> 7      mpg continuo…       min        Min      10.4      1
#> 8      mpg continuo…       max        Max      33.9      1
#> 9      mpg continuo…      skew       skew Right Sk…   <fn>
#>  2 more variables: warning, error