How To Find Iqr In R

5 min read

Finding the interquartile range in R is one of the most practical skills you can learn as a data analyst, because the IQR reveals how spread out the middle 50% of your data is while staying resistant to extreme outliers. This guide walks you through how to find IQR in R using the built-in IQR() function, how it is calculated under the hood, how to handle missing values, and how to use it for outlier detection in real datasets. By the end, you will feel confident calculating the interquartile range in R whether you are working with a simple vector or an entire data frame.

What Is the Interquartile Range (IQR)?

The interquartile range is a measure of statistical dispersion that describes the spread of the central half of a dataset. It is defined as the difference between the third quartile (Q3, the 75th percentile) and the first quartile (Q1, the 25th percentile):

IQR = Q3 − Q1

Unlike the range, which only looks at the maximum minus the minimum, the interquartile range ignores the tails of the distribution. This is exactly why the IQR is considered a solid statistic. If your data contains unusually high or low values, the IQR will not be distorted by them, making it a favorite tool for exploratory data analysis in R Most people skip this — try not to. Which is the point..

Why the IQR Matters in Data Analysis

Understanding the spread of your data is essential before drawing conclusions. The interquartile range helps you:

  • Measure the variability of the middle 50% of observations.
  • Identify outliers using a widely accepted rule.
  • Compare the dispersion of multiple groups or variables.
  • Support boxplot visualizations, where the IQR defines the box itself.

Because it focuses on the center of the data, the IQR gives a clearer picture of consistency than the standard deviation when outliers are present.

How to Find IQR in R Using the IQR() Function

R makes finding the interquartile range incredibly simple with the built-in IQR() function. The basic syntax is:

IQR(x, na.rm = FALSE, type = 7)

Here is what each argument means:

  • x: a numeric vector containing your data.
  • na.rm: a logical value. Set it to TRUE to remove missing values (NA) before calculating.
  • type: the quantile algorithm to use, ranging from 1 to 9. The default is type = 7, which is the method most statistical software uses.

A Basic Example

Suppose you have the following numeric vector of exam scores:

scores <- c(45, 52, 58, 60, 62, 65, 70, 72, 75, 88, 95)

IQR(scores)

R returns the interquartile range for these values. If you want to see the individual quartiles that produce the result, you can use the quantile() function:

quantile(scores, probs = c(0.25, 0.75))

The difference between the 75th percentile and the 25th percentile is precisely the value returned by IQR().

Step-by-Step Process to Calculate IQR in R

If you prefer to understand the calculation manually, you can reproduce the IQR yourself. Follow these steps:

  1. Sort your data from smallest to largest.
  2. Find the first quartile (Q1), the 25th percentile.
  3. Find the third quartile (Q3), the 75th percentile.
  4. Subtract Q1 from Q3 to obtain the interquartile range.

In R, this looks like:

data <- c(12, 15, 22, 28, 33, 37, 41, 49, 55)

q1 <- quantile(data, 0.25)
q3 <- quantile(data, 0.75)
manual_iqr <- q3 - q1

manual_iqr

Compare this with the shortcut:

IQR(data)

Both approaches should give you the same result, confirming that the IQR() function is simply automating the four-step process above.

Handling Missing Values (NA) in R

Real-world datasets are rarely perfect, and missing values can cause the IQR() function to return NA if you are not careful. By default, na.rm = FALSE, so R will not silently ignore gaps in your data Easy to understand, harder to ignore..

To avoid an unexpected NA result, set na.rm = TRUE:

raw_data <- c(10, 14, 22, NA, 31, 45, 50)

IQR(raw_data)
# Returns NA because of the missing value

IQR(raw_data, na.rm = TRUE)
# Now returns the correct IQR, ignoring NA

This is a small detail that makes a huge difference in data cleaning workflows. Whenever you work with imported data, get into the habit of checking for missing values first Simple, but easy to overlook..

Finding IQR for Each Column in a Data Frame

Most analyses do not involve a single vector but an entire data frame with many columns. To calculate the interquartile range for every numeric column, combine IQR() with sapply() or apply() And that's really what it comes down to. No workaround needed..

Using sapply():

df <- data.frame(
  height = c(150, 160, 170, 180, 190),
  weight = c(50, 60, 65, 70, 95),
  age    = c(20, 25, 30, 35, 60)
)

sapply(df, IQR, na.rm = TRUE)

This returns the IQR for height, weight, and age in one clean output, which is perfect for quickly comparing variability across variables.

Using IQR to Detect Outliers

One of the most valuable applications of the interquartile range is outlier detection. The standard rule defines outliers as any values that fall below or above these boundaries:

  • Lower bound = Q1 − 1.5 × IQR
  • Upper bound = Q3 + 1.5 × IQR

Here is how to apply it in R:

data <- c(5, 7, 8, 9, 10, 11, 12, 13, 100)

q1 <- quantile(data, 0.25)
q3 <- quantile(data, 0.75)
iqr_value <- IQR(data)

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

outliers <- data[data < lower | data > upper]
outliers

In this example, the value 100 is flagged as an outlier because it lies far beyond the upper bound. This same logic is what powers the whiskers and points in a boxplot, which you can generate with:

boxplot(data)

Different Quantile Types and Why They Matter

R offers nine quantile algorithms, controlled by the type argument. The default, type = 7, is used by R, Excel

Still Here?

Latest Batch

Dig Deeper Here

You Might Also Like

Thank you for reading about How To Find Iqr In R. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home