Skip to main content
v2026.11,858 entries · CC-BY 4.0

Interquartile Range in R: IQR(), the type Argument and na.rm

Calculate the interquartile range in R with IQR(): the na.rm and type arguments, matching SPSS or Minitab, and why fivenum() can differ.

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.

  1. Put your values in a numeric vector. Example: x = c(2, 4, 6, 8, 10, 12, 14, 16)
  2. Call the function. IQR(x) returns 7 for this vector (lower quartile 5.5, upper quartile 12.5, difference 7).
  3. Handle missing values. If the vector contains NA, set na.rm = TRUE: IQR(x, na.rm = TRUE). The documentation describes na.rm as a logical value for whether missing values should be removed, and its default is FALSE.
  4. 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).
  5. Check against quantile(). The documentation states that IQR(x) is equivalent to quantile(x, 3/4) - quantile(x, 1/4), so unname(quantile(x, 3/4) - quantile(x, 1/4)) gives the same value as IQR(x).
  6. 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)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Interquartile Range in R: IQR(), the type Argument and na.rm

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.