Here we show how to get percentiles, quantiles, quartiles, five number summary and interquartile range in R.

Summary of Functions for Measures of Relative Standing in R
Function Usage
quantile() Derive percentiles, quantiles and quartiles
fivenum() Derive the five number summary
IQR() Derive the interquartile range

All functions come with the "stats" package in the base version of R, hence, no installation is needed.

1 Derive Percentiles in R

The percentile function in R is quantile(). It takes the data and a probability value or a set of probability values as input.

Derive the 50th percentile or median:

Values = c(2, 4, 6, 8, 10, 12, 14, 16, 18)
quantile(Values, 0.5)
50% 
 10 

For other percentile values, the quantile() function also takes a type argument depending on the methodology to be used. The default is type = 7. Here we present 3 commonly used types of the available types 1 to 9.

For \(x_1, x_2, ... ,x_n\) sorted from lowest to highest as \(x_{(1)}, x_{(2)}, ..., x_{(k)}, ... ,x_{(n)}\).

The \(k\)th orders statistic or \(k\)th highest value \(x_{(k)}\) is the following percentile:

Type 4: \(\frac{k}{n}*100\)th percentile.

Type 6: \(\frac{k}{n+1}*100\)th percentile.

Type 7: \(\frac{k-1}{n-1}*100\)th percentile.

Examples using values = c(2, 4, 6, 8, 10, 12, 14, 16, 18) with \(n=9\) values:

The 33.33th percentile with type = 4; which should be 3rd highest value as \(\frac{k}{n}*100 = 33.33 \Rightarrow k = 3\) or \((\frac{3}{9}*100 = 33.33)\).

quantile(Values, 1/3, type = 4)
33.33333% 
        6 

The 40th percentile with type = 6; which should be 4th highest value as \(\frac{k}{n+1}*100 = 40 \Rightarrow k = 4\) or \((\frac{4}{10}*100 = 40)\).

quantile(Values, 0.4, type = 6)
40% 
  8 

The 75th percentile with type = 7; which should be 7th highest value as \(\frac{k-1}{n-1}*100 = 75 \Rightarrow k = 7\) or \((\frac{7-1}{9-1}*100 = 75)\).

quantile(Values, 0.75, type = 7)
75% 
 14 

2 What Percentile is a Number in R?

Using the same type definitions as above:

Values = c(2, 4, 6, 8, 10, 12, 14, 16, 18)

Type 4: \(\frac{k}{n}*100\)th percentile.

# What percentile is 6?
x = 6
length(which(Values <= x))/length(Values)*100
[1] 33.33333

Type 6: \(\frac{k}{n+1}*100\)th percentile.

# What percentile is 8?
x = 8
length(which(Values <= x))/(length(Values)+1)*100
[1] 40

Type 7: \(\frac{k-1}{n-1}*100\)th percentile.

# What percentile is 14?
x = 14
length(which(Values < x))/(length(Values)-1)*100
[1] 75

3 Derive Quantiles in R

Quantiles are the points that split the data range into intervals with equal probabilities.

\(n-\)quantile splits the data into \(n\) intervals of equal probabilities.

Values = c(2, 4, 6, 8, 10, 12, 14, 16, 18)

Derive \(2\)-quantile:

quantile(Values, 1/2)
50% 
 10 

Derive \(3\)-quantile:

quantile(Values, (1:2)/3)

Or:

quantile(Values, c(1/3, 2/3))
33.33333% 66.66667% 
 7.333333 12.666667 

Derive \(5\)-quantile:

quantile(Values, (1:4)/5)

Or:

quantile(Values, c(1/5, 2/5, 3/5, 4/5))
 20%  40%  60%  80% 
 5.2  8.4 11.6 14.8 

4 Derive Quartiles or Five Number Summary in R

Quartiles are also \(4\)-quantile as defined above. The five number summary is the \(4\)-quantile including the extremes (the minimum and maximum values).

The quartiles or \(4\)-quantile values are \(Q_1\) (greater than 25% of the data), \(Q_2\) (greater than 50% of the data), and \(Q_3\) (greater than 75% of the data).

Derive Quartiles:

Values = c(2, 4, 6, 8, 10, 12, 14, 16, 18)
quantile(Values, (1:3)/4)

Or:

quantile(Values, c(1/4, 2/4, 3/4))
25% 50% 75% 
  6  10  14 

Derive five number summary:

The five number summary are \(\text{minimum}\), \(Q_1\), \(Q_2\), \(Q_3\), and \(\text{maximum}\). This is done with the fivenum() function.

Values = c(2, 4, 6, 8, 10, 12, 14, 16, 18)
fivenum(Values)
[1]  2  6 10 14 18

5 Interquartile Range in R

The interquartile range is \(Q_3 - Q_1\) as defined above. This is done with the IQR() function, and it also takes the "type" argument as the quantile() function. The default type is type = 7.

Values = c(2, 4, 6, 8, 10, 12, 14, 16, 18)
IQR(Values)
# Or:
IQR(Values, type =7)
[1] 8

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes