adorn_calc_summ_stats: Add and Calculate Summary Statistics Columns to a Table

View source: R/adorn_calc_summ_stats.R View source: R/adorn_calc_summ_stats.R

adorn_calc_summ_statsR Documentation

Add and Calculate Summary Statistics Columns to a Table

Description

This function formats numeric columns, dynamically adds summary statistics based on a list of formulas, and returns the formatted table.

Usage

adorn_calc_summ_stats(
  tab,
  accuracy = 0.1,
  scale = 1,
  prefix = "",
  suffix = "",
  big_mark = "",
  decimal_mark = ".",
  style_positive = c("none", "plus"),
  style_negative = c("hyphen", "minus", "parens"),
  scale_cut = NULL,
  trim = TRUE,
  form_list = list("{mean} ({sd})", "{p50} [{p25} to {p75}]", "{p0} to {p100}")
)

Arguments

tab

A data frame or tibble containing the data.

accuracy

Numeric. The desired rounding accuracy for summary statistics (default: 0.1).

scale

Numeric. A scaling factor to apply to numeric values (default: 1.0).

prefix

Character. A string to prepend to formatted values (default: "").

suffix

Character. A string to append to formatted values (default: "").

big_mark

Character. The thousands separator (default: "").

decimal_mark

Character. The decimal separator (default: ".").

style_positive

Character. Style for positive numbers. Options: "none", "plus" (default: "none").

style_negative

Character. Style for negative numbers. Options: "hyphen", "minus", "parens" (default: "hyphen").

scale_cut

A function to apply to scaled values for trimming (default: NULL).

trim

Logical. Whether to trim whitespace from formatted numbers (default: TRUE).

form_list

A list of formulas for dynamically adding summary statistic columns (default: list of common summary statistics).

Value

A tibble with formatted and dynamically added columns for summary statistics.

Examples

library(dplyr)
library(ggplot2)  # to get the starwars data set

# descriptive stats for height and mass
starwars %>%
  calc_summ_stats(
    height, mass
  ) |>
  adorn_calc_summ_stats()

# Grouped by gender
starwars %>%
  group_by(gender) %>%
  calc_summ_stats(
    height, mass
  ) |>
  adorn_calc_summ_stats()

# Derive variables within function then summarise
starwars %>%
  calc_summ_stats_t(
    heightm = height / 100,
    bmi = mass / heightm^2
  ) |>
  adorn_calc_summ_stats()

# Grouped by gender
starwars %>%
  group_by(gender) %>%
  calc_summ_stats_t(
    heightm = height / 100,
    bmi = mass / heightm^2
  ) |>
  adorn_calc_summ_stats(form_list = list("{Min}, {Max}"))

emilelatour/lamisc documentation built on March 29, 2025, 1:23 p.m.