How to Calculate Interquartile Range in R

Spread the love

This comprehensive guide is designed to help you understand how to calculate the interquartile range in R.

Understanding the Interquartile Range (IQR)

In statistics, the interquartile range (IQR) is a measure of statistical dispersion and is calculated as the difference between the upper and lower quartiles (Q3 – Q1). The interquartile range is often used as a way to describe the spread of a data set, especially in box-and-whisker plots. It can be used to identify outliers, as any data point that falls below Q1 – 1.5IQR or above Q3 + 1.5IQR is considered an outlier.

Calculating the Interquartile Range in R

R provides several ways to calculate the interquartile range of a given dataset. Here, we will discuss a few methods.

Basic Interquartile Range Calculation

The simplest way to calculate the IQR in R is to use the IQR() function. This function calculates and returns the IQR of a numeric vector.

Here is an example:

# Define a vector
v <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

# Calculate IQR
iqr_v <- IQR(v)


This script will calculate the IQR of the vector and print it.

Interquartile Range Calculation in a Data Frame

When dealing with a data frame, you can calculate the IQR for each column using the sapply() function along with the IQR() function.

Here is an example:

# Create a data frame
df <- data.frame(a = c(1, 2, 3, 4, 5), b = c(6, 7, 8, 9, 10))

# Calculate IQR for each column
iqr_df <- sapply(df, IQR)


This script will calculate the IQR for each column in the data frame and print them.

Handling NA values

If your dataset contains NA (not available) values, the IQR() function will return NA. To ignore the NA values, you need to add na.rm=TRUE to the function.

Here is an example:

# Define a vector with NA values
v <- c(1, 2, 3, NA, 5)

# Calculate IQR
iqr_v <- IQR(v, na.rm = TRUE)


This script will ignore the NA values and return the IQR of the remaining data.

Practical Applications of the Interquartile Range

The IQR is particularly useful in descriptive statistics. It gives you a way to describe the spread of the data in terms of quartiles. It is less affected by outliers and skewed data than other measures like the range, which makes it a more robust measure of dispersion. It is especially useful in boxplots where the box represents the IQR, and whiskers represent the variability outside the lower and upper quartiles.


The R programming language provides several built-in functions for statistical analysis, including the interquartile range. The IQR() function in R is an effective and straightforward tool for measuring the statistical dispersion of a given dataset. However, it’s essential to be mindful of potential NA values within the dataset, as these can affect your result. Therefore, providing the na.rm = TRUE argument within the IQR() function can be a helpful step in real-world data analysis scenarios. By understanding how to calculate the IQR in R, you can begin to analyze the dispersion of your datasets with greater accuracy and detail.

Posted in RTagged

Leave a Reply