Lesson 8 of 8 · 12 min

Describing data with statistics

R has the standard statistics built in:

Function Gives
mean(x) the average
median(x) the middle value (half above, half below)
sd(x) the standard deviation: how spread out the values are
var(x) the variance (sd squared)
quantile(x) min, 25%, median, 75%, max
range(x) min and max
cor(x, y) correlation between two variables (-1 to 1)
table(x) counts of each distinct value
summary(x) a quick overview

Mean or median?

The mean is pulled by extreme values; the median is not. For incomes or house prices, where a few huge values exist, the median describes a "typical" value better. Comparing both is a quick check for skewed data.

Missing values: NA

Real data has gaps, marked NA. Most statistics return NA if any value is missing, which is R being careful. Tell it to skip missing values with na.rm = TRUE, or remove them first:

x <- c(4, NA, 6)
mean(x)                 # NA
mean(x, na.rm = TRUE)   # 5
x[!is.na(x)]            # 4 6

Outliers

A common rule of thumb flags values more than 2 (or 3) standard deviations from the mean as outliers: worth checking, because they might be errors or important exceptions.

Named vectors as results

c(n = 6, mean = 5.2) builds a named vector, a neat way to return several results from one function.

Your task

  1. describe(x): a named vector with n, mean (2 decimals), median, sd (2 decimals), min and max, ignoring NA values.
  2. outliers(x): the values more than 2 standard deviations away from the mean.
script.R
describe <- function(x) {
}

outliers <- function(x) {
}

print(describe(c(2, 4, 4, 5, 7, 9)))

R 4.5 runs right here in your browser. The first run downloads about 25 MB; after that it is instant.

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…