# How to compute quantiles using R with examples

In this tutorial you will learn about the what are quantiles and how to compute quantiles (quartiles, octiles, deciles and percentiles) using R.

## Quantiles using R

Quantiles are the values which divides the entire data into some number of equal parts. The commonly used number of equal parts are 4, 8, 10 and 100.

The general syntax of quantile() function is

quantile(x, probs = seq(0, 1, 0.25), na.rm = FALSE,
names = TRUE, type = 7, ...)

where,

• x: numeric vector
• probs: numeric vector of probabilities with values in [0,1]
• na.rm: logical; if true any NA and NaN's are removed
• names: logical; if true, the result has a names
• type: an integer between 1 and 9 selecting one of the nine quantile algorithms.

In R, quantiles can be calculates using 9 different algorithm. To use specific algorithm, use the argument type with integer value between 1 and 9. For more detail about the 9 different algorithms, check the help of quantile function in R.

## How to compute quantiles using R?

### Quartiles using R

Quartiles are the values which divides the entire data into four equal parts.

x<-c(20,30,21,29,10,17,18,15,27,25,
16,15,19,22,13,17,14,18,12,9)
quantile(x)
   0%   25%   50%   75%  100%
9.00 14.75 17.50 21.25 30.00 

Note that if you do not specify the probs argument in quantile() function, R compute the minimum, first quartile, second quartile, third quartile and the maximum value.

We can use probs=c(0.25,0.50,0.75) argument to get specific quantiles. Below R code compute first, second and third quartile for the data x.

Quartiles <- quantile(x, probs = c(0.25, 0.50, 0.75))
Quartiles
  25%   50%   75%
14.75 17.50 21.25 

If you do not specify the type argument, by default R use type =1 (i.e., inverse of empirical distribution function algorithm) to compute quantiles.

To get quartiles similar to the one obtained using Minitab or SPSS, use the argument type=6.

Quar1 <- quantile(x, probs = c(0.25, 0.50, 0.75), type = 6)
Quar1
  25%   50%   75%
14.25 17.50 21.75 
• The first value is the first quartile $Q_1$ or $25^{th}$ percentile $P_{25}$.
• The second value is the second quartile $Q_2$ or $50^{th}$ percentile $P_{50}$ (same as median).
• The third value is the third quartile $Q_3$ or $75^{th}$ percentile $P_{75}$.
y <- c(x, NA)
quantile(y,probs = c(0.25, 0.50, 0.75), type = 6,na.rm=TRUE)
  25%   50%   75%
14.25 17.50 21.75 

If data contains NA, then use the argument na.rm=TRUE to the quantile function, otherwise R display an error message "missing values and NaN's not allowed if 'na.rm' is FALSE"

### Octiles using R

Octiles are the values that divides the entire data into eight equal parts.

Octiles <-quantile(x,probs=seq(1/8,7/8,1/8),type=6)
Octiles
 12.5%    25%  37.5%    50%  62.5%    75%  87.5%
11.250 14.250 15.875 17.500 19.125 21.750 27.750 
• The first value is the first octile $O_1$.
• The second value is the second octile $O_2$ or $25^{th}$ percentile $P_{25}$.
• Similarly, the last value is the seventh octile $O_7$.

### Deciles using R

Deciles are the values that divides the entire data into ten equal parts.

Deciles <-quantile(x,probs=seq(0.1,0.9,0.1),type=6)
Deciles
 10%  20%  30%  40%  50%  60%  70%  80%  90%
10.2 13.2 15.0 16.4 17.5 18.6 20.7 24.4 28.8 
• The first value is the first decile $D_1$ or $10^{th}$ percentile $P_{10}$.
• The second value is the second decile $D_2$ or $20^{th}$ percentile $P_{20}$.
• Similarly, the last value is the ninth decile $D_9$ or $90^{th}$ percentile $P_{90}$.
Deciles1 <-quantile(x,probs=seq(0.1,0.9,0.1),type=6,names=FALSE)
Deciles1
 10.2 13.2 15.0 16.4 17.5 18.6 20.7 24.4 28.8

If you specify the argument names=FALSE, R display the result without the names.

### Percentiles using R

Percentiles are the values that divides the entire data into hundred equal parts.

Percentiles <-quantile(x,probs=seq(0.1,0.9,0.1),type=6)
Percentiles
 10%  20%  30%  40%  50%  60%  70%  80%  90%
10.2 13.2 15.0 16.4 17.5 18.6 20.7 24.4 28.8 

To get specific percentile we can use the quantile() function with probs argument as follows:

Percent1<-quantile(x,probs=c(0.10,0.35,0.60,0.80),type=6)
Percent1
  10%   35%   60%   80%
10.20 15.35 18.60 24.40 
• The first value is the tenth percentile $P_{10}$.
• The second value is the thirty-fifth percentile $P_{35}$.
• The third value is the $60^{th}$ percentile $P_{60}$.
• The last value is the $80^{th}$ percentile $P_{80}$.

## Endnote

In this tutorial you learned about how to use quantile() function to compute quartiles, octiles, deciles and percentiles using R.

Hopefully you enjoyed learning this tutorial on how to compute quantiles using R. Hope the content is more than sufficient to understand the use of quantile() function in R.