Written and maintained by CASRAI Editorial Board
Last updated
In R, the interquartile range is computed with IQR(x). The function is part of the stats package, takes a numeric vector, and returns the upper quartile minus the lower quartile. Two arguments decide whether the number is right for your data: na.rm, which defaults to FALSE, and type, which defaults to the quantile algorithm 7. This guide covers both arguments, why the result can differ from SPSS or Minitab, and why it can differ from fivenum().
What it is used for in research
The interquartile range (IQR) is a spread measure that is reported next to the median, and it is the quantity behind the box in a box plot. For the concepts themselves see median and interquartile range and percentiles and quartiles; this page is about the R function only.
The R documentation also gives a second use. For normally distributed data the expected IQR is 2 × qnorm(3/4), about 1.3490, so dividing a sample IQR by 1.349 gives a normal-consistent estimate of the standard deviation that is not driven by a few extreme values.
Install or access (official source only)
IQR() is documented in the R reference manual for the stats package, at stat.ethz.ch/R-manual/R-devel/library/stats/html/IQR.html. It belongs to base R’s stats package, so no additional package is needed. R itself is distributed by the R Project at r-project.org.
Step-by-step: compute the IQR in R
The documented usage is IQR(x, na.rm = FALSE, type = 7), where x is a numeric vector.
- Put your values in a numeric vector. Example:
x = c(2, 4, 6, 8, 10, 12, 14, 16) - Call the function.
IQR(x)returns 7 for this vector (lower quartile 5.5, upper quartile 12.5, difference 7). - Handle missing values. If the vector contains
NA, setna.rm = TRUE:IQR(x, na.rm = TRUE). The documentation describesna.rmas a logical value for whether missing values should be removed, and its default isFALSE. - Choose the quantile type if you must match another program.
IQR(x, type = 6)uses quantile algorithm 6. For the vector above it returns 9 (lower quartile 4.5, upper quartile 13.5). - Check against
quantile(). The documentation states thatIQR(x)is equivalent toquantile(x, 3/4) - quantile(x, 1/4), sounname(quantile(x, 3/4) - quantile(x, 1/4))gives the same value asIQR(x). - Compute a robust standard deviation estimate (optional).
IQR(x) / 1.349, following the constant in the documentation.
The worked quartiles above come from applying the two definitions in the quantile documentation to the eight values: type 7 places the k-th ordered value at probability (k-1)/(n-1), and type 6 at k/(n+1), with linear interpolation between neighbouring values.
Output and how to read it
IQR() returns a single number in the same units as your data. Read it as the width of the range containing the middle half of the observations. A larger value means a wider middle half; it says nothing about where that half sits, so report it with the median, or report the two quartiles themselves.
In the example, 7 is the distance between 5.5 and 12.5. Because IQR() collapses the quartiles into one difference, use quantile(x, c(0.25, 0.75)) when a paper or protocol asks for the quartile values. For how quartiles feed outlier rules and box plots, see how to find an outlier in statistics and box plots in R.
Troubleshooting
The result is NA
Because na.rm defaults to FALSE, a vector containing missing values is not silently cleaned. Pass na.rm = TRUE. Be aware that fivenum() has the opposite default: its na.rm is TRUE, so it drops NA and NaN values before computing.
The value does not match SPSS or Minitab
The default for IQR() is type 7, the algorithm used by S and by R before 2.0.0. According to the quantile documentation, type 6 is the algorithm used by Minitab and by SPSS. Try IQR(x, type = 6). The quantile documentation lists nine algorithms from Hyndman and Fan (1996), The American Statistician 50(4), 361-365, who recommended type 8; whichever you choose, state it in your methods section.
The value does not match fivenum()
The IQR documentation says quartiles are computed with quantile() rather than following Tukey’s recommendations. fivenum() returns Tukey’s five-number summary, with hinges instead of quartiles, so the difference of its second and fourth elements can differ from IQR(x). Neither is wrong; they are different definitions. See the IQR and five-number summary for the concept.
Free alternatives
All of these are in base R’s stats package.
| Function | What it returns | How it differs from IQR() |
|---|---|---|
quantile(x, c(0.25, 0.75)) |
The two quartiles | Gives the quartiles themselves; IQR(x) is documented as quantile(x, 3/4) - quantile(x, 1/4). Default type is 7. |
fivenum(x) |
Tukey’s five-number summary: minimum, lower hinge, median, upper hinge, maximum | Uses hinges, not quantile(); na.rm defaults to TRUE. |
FAQ
How do I calculate the interquartile range in R?
Call IQR(x) on a numeric vector. Add na.rm = TRUE if the vector has missing values.
Which quantile type does IQR() use?
Type 7 by default. Use the type argument to select another of the nine algorithms described in the quantile documentation, for example type = 6 to follow the rule documented for Minitab and SPSS.
Is IQR() the same as quantile(x, 0.75) – quantile(x, 0.25)?
Yes, per the documentation, when both use the same type. quantile() returns named values, so wrap the difference in unname() if you want a bare number.
How do I get a standard deviation estimate from the IQR?
Divide IQR(x) by 1.349. The documentation notes that for normal data the expected IQR is 2 × qnorm(3/4), approximately 1.3490.
Sources (official docs)
- R Core Team, IQR: The Interquartile Range, R reference manual,
statspackage. Checked 2026-10-29. - R Core Team, quantile: Sample Quantiles, R reference manual. Checked 2026-10-29.
- R Core Team, fivenum: Tukey Five-Number Summaries, R reference manual. Checked 2026-10-29.
- Hyndman, R. J. and Fan, Y. (1996). Sample quantiles in statistical packages. The American Statistician 50(4), 361-365. https://doi.org/10.1080/00031305.1996.10473566. Checked 2026-10-29.








