ZetCode

R descriptive statistics

last modified November 19, 2026

Descriptive statistics turn a column of numbers into a few values that describe its centre, its spread, and its shape. R ships with the functions that compute them, and the same functions work on a vector, on every column of a data frame, and on groups of rows. This article covers the five-number summary, the mean and the median, variance and standard deviation, quantiles, frequency tables, and grouped summaries with aggregate and tapply.

The five-number summary

summary is the quickest way to look at a numeric vector. It returns the minimum, the first quartile, the median, the mean, the third quartile, and the maximum. The quartiles divide the sorted values into four parts, so a quarter of the observations lie below the first quartile.

five-number.R
scores <- c(72, 85, 90, 66, 78, 95, 81, 69, 88, 74)
print(scores)
print(summary(scores))
cat("length:", length(scores), "\n")

The ten scores run from 66 to 95, with a median of 79.5 and a mean of 79.8. The mean is slightly higher because the two top scores pull it up. The interquartile range, from 72.5 to 87.25, contains the middle half of the values and ignores the extremes by construction.

$ Rscript five-number.R
 [1] 72 85 90 66 78 95 81 69 88 74
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  66.00   72.50   79.50   79.80   87.25   95.00 
length: 10 

Mean, median, and trimming

The mean adds every value and divides by the count, so a single extreme value can move it a long way. The median is the middle value of the sorted data and does not move when an extreme value changes. A trimmed mean drops a fixed fraction from both ends before averaging and sits between the two.

center.R
scores <- c(72, 85, 90, 66, 78, 95, 81, 69, 88, 74)
cat("mean:", mean(scores), "\n")
cat("median:", median(scores), "\n")
cat("trimmed mean:", mean(scores, trim = 0.1), "\n")
scores[2] <- 200
cat("after outlier -> mean:", mean(scores), "\n")
cat("after outlier -> median:", median(scores), "\n")

The mean and the median start close together, at 79.8 and 79.5. Replacing the second score with 200 raises the mean to 91.3 but leaves the median at 79.5, because the middle of the sorted values does not change. The trimmed mean of 79.625 discards ten percent of the values at each end, which is a compromise between the two.

$ Rscript center.R
mean: 79.8 
median: 79.5 
trimmed mean: 79.625 
after outlier -> mean: 91.3 
after outlier -> median: 79.5 

Variance and standard deviation

The variance measures how far the values spread around the mean. R divides the sum of squared deviations by n - 1, the sample variance, which makes it an unbiased estimate of the population variance. The standard deviation is the square root of the variance and is therefore in the same unit as the data.

spread.R
scores <- c(72, 85, 90, 66, 78, 95, 81, 69, 88, 74)
print(range(scores))
cat("variance:", var(scores), "\n")
cat("sd:", sd(scores), "\n")
cat("IQR:", IQR(scores), "\n")
cat("mad:", mad(scores), "\n")

The range is 66 to 95, the variance is about 92.84, and the standard deviation is about 9.64 points. The interquartile range of 14.75 describes the middle half of the scores and is not affected by the two extremes. The median absolute deviation, 11.86, is another robust measure; it is scaled by a constant so that it estimates the same quantity as the standard deviation for normal data.

$ Rscript spread.R
[1] 66 95
variance: 92.84444 
sd: 9.635582 
IQR: 14.75 
mad: 11.8608 

Quantiles

A quantile is the value below which a given fraction of the data lies. quantile returns the minimum, the quartiles, the median, and the maximum by default, and the probs argument requests other fractions. fivenum returns a similar set of five numbers but computes the quartiles with a different rule.

quantiles.R
scores <- c(72, 85, 90, 66, 78, 95, 81, 69, 88, 74)
print(quantile(scores))
print(quantile(scores, probs = c(0.1, 0.9)))
print(fivenum(scores))

The default output repeats the five-number summary from the first section. The tenth percentile is 68.7 and the ninetieth is 90.5, so eighty percent of the scores lie between them. fivenum reports 72 and 88 for the quartiles instead of 72.5 and 87.25, because it uses the hinges of the sorted data rather than the interpolation that quantile applies by default.

$ Rscript quantiles.R
   0%   25%   50%   75%  100% 
66.00 72.50 79.50 87.25 95.00 
 10%  90% 
68.7 90.5 
[1] 66.0 72.0 79.5 88.0 95.0

Frequency tables

table counts the occurrences of each value or combination of values. With two variables it produces a cross-tabulation, and prop.table turns the counts into proportions. The margin argument chooses whether the proportions are computed across rows, across columns, or over the whole table.

tables.R
subject <- factor(c("math", "art", "math", "art", "math", "art", "math", "art"))
grade <- factor(c("A", "B", "B", "A", "C", "B", "A", "C"))
print(table(subject))
print(table(subject, grade))
print(round(prop.table(table(subject, grade), margin = 1), 2))

The subjects are evenly split, with four records each. The cross-table shows that art has one A, two Bs, and one C, while mathematics has two As, one B, and one C. Row proportions sum to one per row: half of the mathematics grades are A, while only a quarter of the art grades are.

$ Rscript tables.R
subject
 art math 
   4    4 
       grade
subject A B C
   art  1 2 1
   math 2 1 1
       grade
subject    A    B    C
   art  0.25 0.50 0.25
   math 0.50 0.25 0.25

A data frame at a glance

summary on a data frame applies the appropriate summary to every column: a five-number summary for numeric columns and a count of the classes for character columns. To compute one statistic across several numeric columns, sapply is shorter than calling the function once per column.

dataframe.R
students <- data.frame(
  name = c("Ada", "Grace", "Alan", "Edsger", "Barbara"),
  score = c(90, 85, 66, 78, 95),
  hours = c(12, 10, 8, 11, 14)
)
print(summary(students))
print(sapply(students[, c("score", "hours")], sd))

The name column reports its length, class, and mode, while score and hours get the numeric summary. The standard deviations are 11.3 points and 2.24 hours, which are in different units and therefore not comparable. A summary of a data frame is a description, not a test; it does not say whether a difference is meaningful.

$ Rscript dataframe.R
     name               score          hours   
 Length:5           Min.   :66.0   Min.   : 8  
 Class :character   1st Qu.:78.0   1st Qu.:10  
 Mode  :character   Median :85.0   Median :11  
                    Mean   :82.8   Mean   :11  
                    3rd Qu.:90.0   3rd Qu.:12  
                    Max.   :95.0   Max.   :14  
    score     hours 
11.300442  2.236068 

Summaries by group

aggregate takes a formula, a data frame, and a function, and applies the function to each group defined by the right side of the formula. tapply does the same for a vector and a grouping factor and returns a named vector or table.

groups.R
students <- data.frame(
  name = c("Ada", "Grace", "Alan", "Edsger", "Barbara", "Donald"),
  score = c(90, 85, 66, 78, 95, 71),
  subject = c("math", "art", "math", "art", "math", "art")
)
print(aggregate(score ~ subject, data = students, FUN = mean))
print(aggregate(score ~ subject, data = students, FUN = sd))
print(tapply(students$score, students$subject, median))

Art averages 78 and mathematics 83.67. The standard deviation is 7 in art and 15.5 in mathematics, so the mathematics scores are much more spread out even though their average is higher. The medians, 78 and 90, show that the mathematics group has a high typical score rather than one outlier pulling the mean up.

$ Rscript groups.R
  subject    score
1     art 78.00000
2    math 83.66667
  subject    score
1     art  7.00000
2    math 15.50269
 art math 
  78   90 

A complete example

The next script describes a vector that contains one missing value. It counts the observed values, computes the centre and the spread with na.rm = TRUE, and reports the coefficient of variation, which divides the standard deviation by the mean and makes spreads comparable across different units.

report.R
scores <- c(72, 85, 90, 66, 78, 95, 81, 69, 88, 74, NA)
cat("observed:", sum(!is.na(scores)), "\n")
cat("mean:", mean(scores, na.rm = TRUE), "\n")
cat("median:", median(scores, na.rm = TRUE), "\n")
cat("sd:", round(sd(scores, na.rm = TRUE), 2), "\n")
print(quantile(scores, probs = c(0.25, 0.75), na.rm = TRUE))
cat("cv:", round(sd(scores, na.rm = TRUE) / mean(scores, na.rm = TRUE), 3), "\n")

Ten values are observed, the mean is 79.8, and the median is 79.5. The standard deviation of 9.64 and the quartiles of 72.5 and 87.25 describe the same spread as before, now computed on the values that exist. The coefficient of variation is 0.121, so the standard deviation is about twelve percent of the mean; values above one would indicate a spread as large as the centre itself.

$ Rscript report.R
observed: 10 
mean: 79.8 
median: 79.5 
sd: 9.64 
  25%   75% 
72.50 87.25 
cv: 0.121 

Next steps

The probability distributions article turns these summaries into models and computes probabilities from them. The base graphics article draws the histogram and boxplot that show the same numbers visually, and the missing values article explains why na.rm is needed in the final example. The apply family article covers the functions that the grouped summaries are built on.

Source

R documentation: summary

R documentation: standard deviation and variance

R documentation: quantiles

R documentation: table

R documentation: aggregate

In this article we have summarised a vector with summary and compared the mean with the median and a trimmed mean. We measured spread with var, sd, IQR, and mad, read quantiles, and built frequency and proportion tables. We summarised a data frame column by column and grouped rows with aggregate and tapply, then computed a coefficient of variation with a missing value present. The series continues with R probability distributions.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all R Language tutorials.