summaryStats: Summary Statistics

View source: R/summaryStats.R

summaryStatsR Documentation

Summary Statistics

Description

Produces a table of summary statistics for the data. If the argument group is missing, calculates a matrix of summary statistics for the data in x. If group is present, the elements of group are interpreted as group labels and the summary statistics are displayed for each group separately.

Usage

summaryStats(x, ...)

## Default S3 method:
summaryStats(
  x,
  group = rep("Data", length(x)),
  data.order = TRUE,
  digits = 2,
  ...
)

## S3 method for class 'formula'
summaryStats(x, data = NULL, data.order = TRUE, digits = 2, ...)

## S3 method for class 'matrix'
summaryStats(x, data.order = TRUE, digits = 2, ...)

Arguments

x

either a single vector of values, a formula of the form data ~ group, or a matrix.

...

Optional arguments that are passed to the summary statistic functions. For example na.rm = TRUE will help if there are missing values in the (response) variable.

group

a vector of group labels.

data.order

if TRUE, the group order is the order in which the groups are first encountered in group. If FALSE, the order is alphabetical.

digits

the number of decimal places to display.

data

an optional data frame containing the variables in the model.

Value

A teaching summary is printed as a side effect. The returned value is invisible so that classroom use can focus on the printed summary while programmatic use can still save the result.

If x is a single variable and no grouping is supplied, an invisible list is returned with the following named items:

min

Minimum value.

max

Maximum value.

mean

Mean value.

var

Variance – the average of the squares of the deviations of the data values from the sample mean.

sd

Standard deviation – the square root of the variance.

n

Number of data values – size of the dataset.

nMissing

If there are missing values, and na.rm has been set to TRUE, the number of missing values.

iqr

Midspread (IQR) – the range spanned by the central half of the data; the interquartile range.

skewness

Skewness statistic – indicates how skewed the data set is. Positive values indicate right-skew data. Negative values indicate left-skew data.

lq

Lower quartile.

median

Median – the middle value when the batch is ordered.

uq

Upper quartile.

If grouping is provided, either by using the group argument, by using a formula, or by passing a matrix whose columns represent groups, the function invisibly returns a data.frame with one row for each group and columns containing the summary statistics.

Methods (by class)

  • summaryStats(default): Summary Statistics

  • summaryStats(formula): Summary Statistics

  • summaryStats(matrix): Summary Statistics

Examples


## STATS20x data:
data(course.df)

## Single variable summary
with(course.df, summaryStats(Exam))

## Using a formula
summaryStats(Exam ~ Stage1, course.df)

## Using a matrix
courseMatrix = cbind(course.df$Exam, course.df$Assign, course.df$Test)
summaryStats(courseMatrix)

## Saving and extracting the information
sumStats = summaryStats(Exam ~ Degree, course.df)
sumStats

## Just the BAs
sumStats['BA', ]

## Just the means
sumStats$mean


s20x documentation built on July 1, 2026, 9:06 a.m.