Coefficient of variation in R: when it beats the standard deviation

The author works for, or receives funding or products from, an organization that benefits from this article.

The standard deviation tells you how far values spread around their mean, in the units of the data. That is exactly why it cannot compare two variables measured on different scales. The coefficient of variation (CV) fixes this by dividing the standard deviation by the mean:

CV = (standard deviation / mean) x 100

The result is a percentage with no units, so a CV of 15% means the same thing for millimetres, kilograms or dollars. In this tutorial we compute the CV in base R, use it on the built-in iris dataset, and look at the three cases where it misleads.

A CV function in base R

R has no cv() in the base packages, but it takes three lines:

cv <- function(x, na.rm = TRUE) {
  sd(x, na.rm = na.rm) / mean(x, na.rm = na.rm) * 100
}

sd() in R uses the sample formula (divide by n – 1), so this is the sample CV.

Which iris measurement varies the most?

The iris data has four measurements in centimetres. Let's compare their standard deviations and CVs:

means <- sapply(iris[, 1:4], mean)
sds   <- sapply(iris[, 1:4], sd)
cvs   <- sapply(iris[, 1:4], cv)
for (n in names(sds)) cat(n, "mean", round(means[n], 4), "sd", round(sds[n], 4),
                          "cv", round(cvs[n], 2), "n")
Sepal.Length mean 5.8433 sd 0.8281 cv 14.17
Sepal.Width  mean 3.0573 sd 0.4359 cv 14.26
Petal.Length mean 3.758  sd 1.7653 cv 46.97
Petal.Width  mean 1.1993 sd 0.7622 cv 63.56

Judged by standard deviation, Petal.Length (1.7653) varies the most. Judged by CV, Petal.Width does (63.56%), because its mean is small: its spread is large relative to its typical size.

cat("largest SD:", names(which.max(sds)), "| largest CV:", names(which.max(cvs)), "n")
largest SD: Petal.Length | largest CV: Petal.Width

CV by group

Variability usually differs between groups. We compute the CV of the petal width for each species:

res <- aggregate(cbind(Petal.Length, Petal.Width) ~ Species, data = iris, FUN = cv)
round(res$Petal.Width, 2)
42.84 14.91 13.56

That is setosa, versicolor and virginica in order. Setosa petals are tiny (mean 0.246 cm, sd 0.105), so a spread that looks small in centimetres is a 42.84% spread relative to the mean. For comparison, setosa's Petal.Length has a CV of only 11.88%.

Sample or population CV

sd() divides by n – 1. If your data are the whole population, divide by n instead:

x <- iris$Petal.Width[1:50]   # setosa
sqrt(mean((x - mean(x))^2)) / mean(x) * 100
42.41

The sample CV is 42.84 and the population CV is 42.41. The gap shrinks as n grows.

Three ways the CV misleads

It needs a true zero. Temperature in degrees Celsius has an arbitrary zero, so the CV changes when you change the scale:

cc <- c(10, 15, 20)
ff <- cc * 9 / 5 + 32
cat("C: sd", round(sd(cc), 4), "cv", round(cv(cc), 2),
    "| F: sd", round(sd(ff), 4), "cv", round(cv(ff), 2), "n")
C: sd 5 cv 33.33 | F: sd 9 cv 15.25

The same temperatures give a CV of 33.33% in Celsius and 15.25% in Fahrenheit. Only use the CV on ratio data such as lengths, weights, counts and prices.

It explodes near a zero mean. When the mean is 0, the CV is undefined:

z <- c(-2, -1, 0, 1, 2)
cat("mean", mean(z), "sd", round(sd(z), 4), "cv", cv(z), "n")
mean 0 sd 1.5811 cv Inf

A mean close to zero gives a huge, unstable CV. Data that mix positive and negative values are a poor fit.

It depends on the sampling. With small samples, the CV is biased and noisy, so treat a single small-sample CV as a rough guide.

Summary

  • The CV (sd / mean x 100) puts spread on a unit-free scale, so you can compare variables measured in different units.
  • In iris, the standard deviation points to Petal.Length as the most variable measurement, but the CV points to Petal.Width.
  • sd() gives the sample CV; divide by n yourself for a population CV.
  • Use the CV only on ratio data with a mean well away from zero.

Session info: R version 4.6.1 (2026-06-24).

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.