To calculate the interquartile range in R, you can use IQR() function.This interquartile range gives range between first quartile and third quartile of a dataset.

The following method shows how you can do it with syntax.

Method: Use IQR() Function

IQR(data)


The following example shows how to calculate interquartile range using IQR() in R.

## Use IQR() to Calculate Interquartile Range for Dataset

Let’s see how we can use IQR() function to calculate interquartile range:

# Create dataset
data <- c(78,85,84,81,79,85,85,81,78,89,84,84,80)

# Find interquartile range
range <- IQR(data)

# Show interquartile range
print(range)


Output:

[1] 5


Here the output shows interquartile range is 5 for above dataset.

## Use IQR() to Calculate Interquartile Range for Data Frame

Let’s see how we can calculate interquartile range for one of the column of data frame:

# Create data frame
df <- data.frame(Pressure=c(12.39,11.25,12.15,13.48,13.78,12.89,12.21,12.58),
Temperature=c(78,89,85,84,81,79,77,85),
Humidity=c(5,7,1,2,7,8,9,4))

# Find interquartile range
range <- IQR(df\$Temperature)

# Show interquartile range
print(range)


Output:

[1] 6.25


As the output shows interquartile range is 6.25 for Temperature column of data frame.