# How do you calculate IQR (interquartile range)?

I have the following data (ordered):

$$0, 1, 1, 2, 3, 4, 4, 7, 9, 23.$$

As far as I know, $Q_1 \text{(median of the upper half)} = 1$; $Q_3 \text{(median of the lower half)} = 7$; Therefore, $\text{IQR} = Q_3-Q_1 = 6$.

But when I boxplot this simple data in 'R', the summary says that, $Q_1 = 1.25$, $Q_3 = 6.25$, and consequently, $\text{IQR} = 6.25-1.25 = 5$!! How is that? I don't think we can question the statistical computation by "R" anyway ... Can anyone tell me what is wrong?

• This question is probably better suited for stackoverflow: the answer has nothing to do with Mathematics, only the way R calculates the IQR. – George V. Williams Feb 15 '13 at 17:48
• Maybe you should read the R manual to see how it computes the quartiles? Note that your interquartile range includes $7$ or $6$ of the numbers in your data set (including the endpoints), while R's range had $5$ out of the $10$ numbers in your data set. – Dilip Sarwate Feb 15 '13 at 17:48

## 2 Answers

R uses the quantile function to do IQR:

Note that this function computes the quartiles using the quantile function rather than following Tukey's recommendations, i.e., $IQR(x) = \mathrm{quantile}(x, 3/4) - \mathrm{quantile}(x, 1/4)$.

• What is Tukey's recommendation? - what you wrote in the "i.e" or something other than that? – alancalvitti Feb 15 '13 at 18:08
• I didn't write that, that's a quote from R's documentation. Tukey's recommendation is the $Q_3 - Q_1$. However, there's other definitions of the interquartile range than that. – George V. Williams Feb 15 '13 at 18:20
• I noticed it here: inside-r.org/r-doc/stats/IQR – ToNoY Feb 15 '13 at 18:20
• @ToNoY, Like I said, Tukey's recommendation is $Q_3 - Q_1$, just like you did. However R does something different to compute the IQR. – George V. Williams Feb 15 '13 at 18:21
• @GeorgeV.Williams: Thanks, I just found it in detail from a answer in superuser as you can see from the link below. But is it worth doing it by hand the way R or excel does the computation? – ToNoY Feb 15 '13 at 18:33

The answer can be found here (excel also does the same thing):

https://superuser.com/questions/343339/excel-quartile-function-doesnt-work