Your difficulty is that in order to find IQR you must first find the
two quartiles. And there are many
different formulas for quantiles (including quartiles) in common use.
In particular, major statistical software packages disagree on which methods to implement as their default: (a) SAS, (b) Minitab and SPSS, and (c) R (and its parent S) use three different methods. Furthermore, these methods differ from methods found in reputable elementary texts. (Adding to the confusion: Tukey's 'fourths', sometimes used in making boxplots and often considered essentially the same as quartiles, use yet other criteria.)
Generally speaking the differences among these methods become negligible for large sample sizes. However, there can be marked differences for small samples. Fortunately, it is for large samples that quantiles make the most sense. (Roughly, quartiles are intended to divide a sample into four 'chunks' of equal size: how do you do that with a sample of size 10?)
In R, you can type ? quantile
to see the nine different types of
quantiles supported by R (using an extra argument), mentioned just now
by @EdM. The default result from quantile is min, Q1, med, Q3, max, so once you have selected a type
you could define your own IQR function based on the idea in the @Glen_b Comment and code like as.numeric(diff(quantile(x, type=5)[c(2,4)]))
.
The standard deviation is the square root of the variance, as you might know. The variance is calculated by summing up the squared deviation from the mean, and dividing it by $n$.
$$\sigma^2 = \frac{\sum_{i}^{n} (x_i-\mu)^2 }{n}$$
Every difference $(x-\mu)$ is squared. When you take the square root of the variance, it is not the same as taking the square root of every $(x-\mu)^2$ and sum it up afterwards...
Because of the square term, the variance (and thus the standard deviation) gives more weight to more distant values and can't be negative, as positive and negative values get both positive when squared.
It is also wrong to calculate the standard deviation for all positive and negative values separately. The values lose their sign when they get squared.
Hope this helps,
Best Answer
The IQR and standard deviation both are proportional to a scale factor, so the proper way to compare the two is with their ratio.
Upper bound for SD:IQR
The Cauchy distribution with PDF
$$\frac{dx / \sigma}{\pi(1 + (x/\sigma)^2)}$$
has infinite SD and quartiles at $\pm\sigma$. From it we can create, via truncation on the left and right, a distribution with arbitrarily large SD while (by adjusting $\sigma$) we can separately make the IQR arbitrarily short. Therefore, for any given IQR there is no upper bound on the SD and for any given SD there is no lower bound on the IQR.
Lower bound for SD:IQR
For any given IQR, we can reduce the SD in two ways: (1) by shifting the middle 50% of the values towards the mid-point of the quartiles and (2) by shifting the outer 50% of the values towards the quartiles. The lower limit of the SD for a fixed IQR is achieved by the family of (discrete) distributions having $25 + \varepsilon$% probability at $-1$ and $1$ and $50 - 2\varepsilon$% probability at $0$ ($0 \lt \varepsilon \lt 25$); members of this family have quartiles at $\pm 1$--whence an IQR of $2$ and SDs of $(50 + 2\varepsilon)/100$; the (lower) limiting ratio of SD to IQR therefore is $1/4$.
(Notice that no member of this family violates Chebyshev's Inequality, provided some care is taken in its statement: $100$% of the probability lies strictly within 2 SDs of the mean ($0$) in every case and in every case there is no ambiguity concerning the positions of the quartiles. However, in the limit as $\varepsilon \to 0$, the ratio of SD to IQR approaches $1/4$. Incorrectly interpreted, this would seem to imply that $50$% of the probability lies beyond $2$ SDs of the mean, whereas Chebyshev's Inequality asserts that no more than $25$% of the probability can lie beyond $2$ SDs of the mean. However, the positions of the quartiles for the limiting distribution with $\varepsilon=0$ are ambiguous: the lower one could be anywhere between $-1$ and $0$ and the upper anywhere between $0$ and $1$ and none of the probability is strictly beyond $2$ SDs from the mean.)
Summary
Because the empirical distribution of a sufficiently large finite sample can approach any given distribution arbitrarily closely, the conclusion--both for theoretical distributions and empirical distributions of data--is that
$$\frac{1}{4} \le \frac{SD}{IQR} \le \infty$$
and these are the best bounds possible.