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
describe(x): a named vector withn,mean(2 decimals),median,sd(2 decimals),minandmax, ignoringNAvalues.outliers(x): the values more than 2 standard deviations away from the mean.