rguides

How to Detect Outliers in a Vector in R

You can detect outliers in R quickly using the Interquartile Range (IQR) method, a routine step before running statistical tests or building models. The IQR approach flags any value more than 1.5×IQR below Q1 or above Q3, making it resistant to the influence of a single extreme point.

values <- c(10, 12, 14, 15, 16, 17, 18, 19, 100)

q1 <- quantile(values, 0.25)
q3 <- quantile(values, 0.75)
iqr <- q3 - q1

lower <- q1 - 1.5 * iqr
upper <- q3 + 1.5 * iqr

outliers <- values[values < lower | values > upper]
outliers
# [1] 100

The IQR method uses order statistics, so a single extreme value won’t ruin the threshold the way it would with mean-based approaches like the z-score. For a quick one-liner, boxplot.stats(values)$out returns the same result using the identical 1.5×IQR rule. If you need visual confirmation, boxplot(values) draws the plot and marks outliers as dots beyond the whiskers.

boxplot.stats(values)$out
# [1] 100

Outlier detection doesn’t tell you what to do with the outliers — that depends on your domain. In some fields you remove them; in others they are the interesting data points worth investigating separately.

See also