Basic Statistics
R ships with the everyday statistics functions built in — mean(), median(), sd(), and a five-number summary in a single call.
mean, median, and standard deviation
R stats.R
scores <- c(88, 92, 79, 95, 84) mean(scores) median(scores) sd(scores)
Console
[1] 87.6 [1] 88 [1] 6.107373
summary(): five numbers at once
R summary.R
summary(scores)
Console
Min. 1st Qu. Median Mean 3rd Qu. Max.
79 84 88 87.6 92 95 summary() hands back the minimum, the 25th and 75th percentiles (the first and third quartiles), the median, the mean, and the maximum — a quick shape of your data without calling five separate functions.
NA breaks these functions by default
R na-stats.R
mixed <- c(88, NA, 92) mean(mixed) mean(mixed, na.rm = TRUE)
Console
[1] NA [1] 90
Note: Just like plain arithmetic, statistics functions propagate
NA by default rather than silently skipping it — a single missing value is enough to turn mean() into NA. The na.rm = TRUE argument (short for "NA remove"), supported by mean(), median(), sd(), and most other summary functions, tells R to ignore NAs and compute over what's left instead.