How do you calculate the interquartile range in R?

How do you calculate the interquartile range in R?

The interquartile range in R is a measure of spread or variability in a dataset. It represents the difference between the third and first quartiles, and it is calculated by finding the middle 50% of the data. To calculate the interquartile range in R, first, the dataset must be arranged in ascending order. Then, the third quartile (75th percentile) and first quartile (25th percentile) can be determined using the quantile function. Finally, the difference between these two quartiles is the interquartile range. This calculation can be easily performed using built-in functions in R, making it a useful tool for analyzing and comparing data sets.

Calculate Interquartile Range in R (With Examples)


The interquartile range represents the difference between the first quartile (the 25th percentile) and the third quartile (the 75th percentile) of a dataset.

In simple terms, it measures the spread of the middle 50% of values.

IQR = Q3 – Q1

We can use the built-in IQR() function to calculate the interquartile range of a set of values in R:

IQR(x)

The following examples show how to use this function in practice.

Example 1: Interquartile Range of a Vector

The following code shows how to calculate the interquartile range of values in a vector:

#define vector
x <- c(4, 6, 6, 7, 8, 12, 15, 17, 20, 21, 21, 23, 24, 27, 28)

#calculate interquartile range of values in vector
IQR(x)

[1] 14.5

Example 2: Interquartile Range of a Vector with Missing Values

If your vector has missing values, be sure to specify na.rm=TRUE to ignore missing values when calculating the interquartile range:

#define vector with some missing values
x <- c(4, 6, NA, 7, NA, NA, 15, 17, 20, 21, 21, 23, 24, 27, 28)

#calculate interquartile range of values in vector
IQR(x, na.rm=TRUE)

[1] 10.25

Example 3: Interquartile Range of Column in Data Frame

The following code shows how to calculate the interquartile range of a specific column in a data frame:

#define data frame
df <- data.frame(var1=c(1, 3, 3, 4, 5),
                 var2=c(7, 7, 8, 3, 2),
                 var3=c(3, 3, 6, 6, 8),
                 var4=c(1, 1, 2, 8, 9))

#calculate interquartile range of 'var1' column
IQR(df$var1)

[1] 1

Example 4: Interquartile Range of Several Columns in Data Frame

The following code shows how to calculate the interquartile range of several columns in a data frame:

#define data frame
df <- data.frame(var1=c(1, 3, 3, 4, 5),
                 var2=c(7, 7, 8, 3, 2),
                 var3=c(3, 3, 6, 6, 8),
                 var4=c(1, 1, 2, 8, 9))

#calculate interquartile range of 'var1', 'var2', and 'var4' columns
sapply(df[ , c('var1', 'var2', 'var4')], IQR)

var1 var2 var4 
   1    4    7

Cite this article

stats writer (2024). How do you calculate the interquartile range in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-calculate-the-interquartile-range-in-r/

stats writer. "How do you calculate the interquartile range in R?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-do-you-calculate-the-interquartile-range-in-r/.

stats writer. "How do you calculate the interquartile range in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-calculate-the-interquartile-range-in-r/.

stats writer (2024) 'How do you calculate the interquartile range in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-calculate-the-interquartile-range-in-r/.

[1] stats writer, "How do you calculate the interquartile range in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How do you calculate the interquartile range in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top