# Understanding The Algorithms Behind Quantile() in R

So I was trying to compute the quantiles from a dataset in R:

c(3,5,7,8,12,13,14,18,21)


using the quantile() function, and I realised that it was returning unexpected quartile values for Q1 and Q3 (7 and 14, respectively, should be 6 and 16) on it's own. So I went into the documentation and saw you can assign a type to the function 1-9 that correspond to algorithms. By process of elimination type 6 yields the expected quantiles for that specific dataset, but due to my weak maths I don't actually understand the algorithms being used, and why type 6 specifically worked on my data type (more on those). Does a good explanation for when to use each type exist for someone really new to stats?

• What is unclear for you? – Tim Jan 9 '16 at 20:32
• – gung - Reinstate Monica Jan 9 '16 at 20:35
• As you found, there are many ways to calculate quantiles. – William Chiu Jan 9 '16 at 20:39
• @Kendall why do you consider this result to be "wrong"? – Tim Jan 9 '16 at 22:20
• Some useful threads can be found by searching our site. The one at stats.stackexchange.com/questions/13399 seems like it might be particularly helpful. – whuber Jan 9 '16 at 23:40

Why should the answers be $6$ and $16$? The $p^\text{th}$ quantile $q_p$ is that value for which the proportion of the sample less than or equal to $q_p$ is at least $p$, and the proportion greater than or equal to $q_p$ is at least $1 - p$. If we take $p = 1/4$ and look at your sample $\{3, 5, 7, 8, 12, 13, 14, 18, 21 \}$, $7$ matches this definition but $6$ doesn't. The same is true for the $p = 3/4$ quantile.