Here we show how to get percentiles, quantiles, quartiles, five number summary and interquartile range 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.
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:
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)\).
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)\).
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)\).
75%
14
Using the same type definitions as above:
Type 4: \(\frac{k}{n}*100\)th percentile.
[1] 33.33333
Type 6: \(\frac{k}{n+1}*100\)th percentile.
[1] 40
Type 7: \(\frac{k-1}{n-1}*100\)th percentile.
[1] 75
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.
Derive \(2\)-quantile:
50%
10
Derive \(3\)-quantile:
Or:
33.33333% 66.66667%
7.333333 12.666667
Derive \(5\)-quantile:
Or:
20% 40% 60% 80%
5.2 8.4 11.6 14.8
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:
Or:
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.
[1] 2 6 10 14 18
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.
[1] 8
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes