1. Descriptive Statistics in R

Descriptive statistics give you a first look at your data. Before running any model, you need to understand the shape, centre, and spread of your variables.

Setup Link to heading

library(tidyverse)

# We'll use the built-in mtcars dataset
data(mtcars)
head(mtcars)

Measures of Central Tendency Link to heading

Mean, Median, Mode Link to heading

# Mean
mean(mtcars$mpg)

# Median
median(mtcars$mpg)

# Mode (no built-in function)
get_mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}
get_mode(mtcars$cyl)

When to use which? Link to heading

MeasureBest forSensitive to outliers?
MeanSymmetric distributionsYes
MedianSkewed distributionsNo
ModeCategorical dataNo

Measures of Dispersion Link to heading

# Standard deviation
sd(mtcars$mpg)

# Variance
var(mtcars$mpg)

# Range
range(mtcars$mpg)
diff(range(mtcars$mpg))

# Interquartile range
IQR(mtcars$mpg)

# Quantiles
quantile(mtcars$mpg, probs = c(0.25, 0.5, 0.75))

Summary Statistics at a Glance Link to heading

# Base R summary
summary(mtcars$mpg)

# More detailed summary with psych package
# install.packages("psych")
library(psych)
describe(mtcars$mpg)

Frequency Tables Link to heading

# For categorical/discrete variables
table(mtcars$cyl)

# Proportions
prop.table(table(mtcars$cyl))

# Cross-tabulation
table(mtcars$cyl, mtcars$gear)

Group-wise Summaries Link to heading

mtcars %>%
  group_by(cyl) %>%
  summarise(
    n       = n(),
    mean_mpg = mean(mpg),
    sd_mpg   = sd(mpg),
    median_mpg = median(mpg),
    min_mpg  = min(mpg),
    max_mpg  = max(mpg)
  )

Checking Normality Link to heading

# Shapiro-Wilk test
shapiro.test(mtcars$mpg)

# Q-Q plot
qqnorm(mtcars$mpg)
qqline(mtcars$mpg, col = "red")

Skewness and Kurtosis Link to heading

library(moments)
# install.packages("moments")

skewness(mtcars$mpg)
kurtosis(mtcars$mpg)

A skewness near 0 indicates symmetry. Kurtosis near 3 indicates a normal-like tail weight (excess kurtosis near 0).

Key Takeaways Link to heading

  • Always start with descriptive statistics before modelling.
  • Use summary() and describe() for a quick overview.
  • Check normality with Shapiro-Wilk and Q-Q plots.
  • Group-wise summaries using dplyr reveal differences across subgroups.

Next: Data Visualisation →