Learn R Programming

rstatix (version 1.1.0)

get_summary_stats: Compute Summary Statistics

Description

Compute summary statistics for one or multiple numeric variables.

See the Datanovia tutorial Descriptive Statistics in R for a worked walkthrough.

Usage

get_summary_stats(
  data,
  ...,
  type = c("full", "common", "robust", "five_number", "mean_sd", "mean_se", "mean_ci",
    "median_iqr", "median_mad", "quantile", "mean", "median", "min", "max"),
  show = NULL,
  probs = seq(0, 1, 0.25),
  digits = 3
)

Value

A data frame containing descriptive statistics, such as:

  • n: the number of individuals

  • min: minimum

  • max: maximum

  • median: median

  • mean: mean

  • q1, q3: the first and the third quartile, respectively.

  • iqr: interquartile range

  • mad: median absolute deviation (see ?MAD)

  • sd: standard deviation of the mean

  • se: standard error of the mean

  • ci: 95 percent confidence interval of the mean

When requested through show, the output can also contain:

  • skewness: bias-corrected sample skewness

  • kurtosis: bias-corrected sample excess kurtosis (0 for a normal distribution).

Both use the type-2 (bias-corrected) estimator, matching

e1071 with type = 2: skewness \(= g_1\sqrt{n(n-1)}/(n-2)\) and kurtosis \(= [(n+1)g_2 + 6] (n-1)/[(n-2)(n-3)]\), where \(g_1 = m_3/m_2^{1.5}\) and \(g_2 = m_4/m_2^2 - 3\). Skewness is NA for n < 3 and kurtosis for n < 4.

Arguments

data

a data frame

...

(optional) One or more unquoted expressions (or variable names) separated by commas. Used to select a variable of interest. If no variable is specified, then the summary statistics of all numeric variables in the data frame is computed.

type

type of summary statistics. Possible values include: "full", "common", "robust", "five_number", "mean_sd", "mean_se", "mean_ci", "median_iqr", "median_mad", "quantile", "mean", "median", "min", "max"

show

a character vector specifying the summary statistics you want to show. Example: show = c("n", "mean", "sd"). This is used to filter the output after computation. It can additionally include "skewness" and/or "kurtosis" (e.g. show = c("mean", "sd", "skewness", "kurtosis")); these two are computed on demand and are not part of any default type.

probs

numeric vector of probabilities with values in [0,1]. Used only when type = "quantile".

digits

integer indicating the number of decimal places to round the summary statistics to. Default is 3. Increase it when summarizing very small values that would otherwise round to 0.

See Also

rstatix-programming for selecting columns by names held in strings (!!, {{ }}, vars=, all_of()). The Datanovia tutorial: Descriptive Statistics in R.

Examples

Run this code
# Full summary statistics
data("ToothGrowth")
ToothGrowth %>% get_summary_stats(len)

# Summary statistics of grouped data
# Show only common summary
ToothGrowth %>%
  group_by(dose, supp) %>%
  get_summary_stats(len, type = "common")

# Robust summary statistics
ToothGrowth %>% get_summary_stats(len, type = "robust")

# Five number summary statistics
ToothGrowth %>% get_summary_stats(len, type = "five_number")

# Compute only mean and sd
ToothGrowth %>% get_summary_stats(len, type = "mean_sd")

# Compute full summary statistics but show only mean, sd, median, iqr
ToothGrowth %>%
    get_summary_stats(len, show = c("mean", "sd", "median", "iqr"))

# Include skewness and kurtosis (computed on demand via show)
ToothGrowth %>%
    get_summary_stats(len, show = c("mean", "sd", "skewness", "kurtosis"))

Run the code above in your browser using DataLab