Skip to content
AgileLogics
6 min read

Descriptive Statistics

Prerequisites: Working with vectors, Missing values: NA and NULL

Descriptive statistics summarize data: where the values are centred, how spread out they are, and how variables relate.

Centre and spread

times <- c(12, 15, 11, 18, 14, 45, 13)

mean(times)
median(times)
sd(times)
IQR(times)
quantile(times)
[1] 18.28571
[1] 14
[1] 11.99603
[1] 4
  0%  25%  50%  75% 100%
11.0 12.5 14.0 16.5 45.0
  • The mean (average) is pulled up by the single value of 45; the median (middle value) isn't. When data has outliers or is skewed — incomes, house prices, response times — the median often describes a "typical" value better.
  • The standard deviation (sd) measures typical distance from the mean. The interquartile range (IQR) is the spread of the middle 50%, and like the median, resists outliers.

Counting categories

answers <- c("yes", "no", "yes", "yes", "unsure", "no", "yes")
counts <- table(answers)
counts
round(prop.table(counts) * 100, 1)
answers
    no unsure    yes
     2      1      4
answers
    no unsure    yes
  28.6   14.3   57.1

prop.table() turns counts into proportions.

Relationships

hours <- c(1, 2, 3, 4, 5, 6)
score <- c(52, 58, 65, 70, 74, 83)
round(cor(hours, score), 3)

df <- data.frame(group = c("a", "a", "b", "b"), result = c("pass", "fail", "pass", "pass"))
table(df$group, df$result)
[1] 0.995

    fail pass
  a    1    1
  b    0    2
  • Correlation (cor) ranges from -1 to 1; values near 1 mean a strong positive straight-line relationship. Correlation doesn't show that one thing causes the other.
  • A two-way table (a cross-tabulation) counts combinations of two categories.

Summaries by group with dplyr

library(dplyr, warn.conflicts = FALSE)

tibble(group = c("a", "a", "a", "b", "b"), value = c(10, 12, 14, 20, 30)) |>
  summarise(n = n(), mean = mean(value), median = median(value), sd = sd(value), .by = group)
# A tibble: 2 × 5
  group     n  mean median    sd
  <chr> <int> <dbl>  <dbl> <dbl>
1 a         3    12     12  2
2 b         2    25     25  7.07

Common mistakes

  • Reporting only the mean for skewed data. Report the median too, and look at the distribution with a histogram before summarizing.
  • Forgetting na.rm = TRUE.