Learn R Programming

EnvStats (version 2.1.0)

iqr: Interquartile Range

Description

Compute the interquartile range for a set of data.

Usage

iqr(x, na.rm = FALSE)

Arguments

x
numeric vector of observations.
na.rm
logical scalar indicating whether to remove missing values from x. If na.rm=FALSE (the default) and x contains missing values, then a missing value (NA) is returned. If na.rm=TRUE,

Value

  • A numeric scalar -- the interquartile range.

Details

Let $\underline{x}$ denote a random sample of $n$ observations from some distribution associated with a random variable $X$. The sample interquartile range is defined as: $$IQR = \hat{X}_{0.75} - \hat{X}_{0.25} \;\;\;\;\;\; (1)$$ where $X_p$ denotes the $p$'th quantile of the distribution and $\hat{X}_p$ denotes the estimate of this quantile (i.e., the sample $p$'th quantile). See the Rhelp file for quantile for information on how sample quantiles are computed.

References

Chambers, J.M., W.S. Cleveland, B. Kleiner, and P.A. Tukey. (1983). Graphical Methods for Data Analysis. Duxbury Press, Boston, MA. Cleveland, W.S. (1993). Visualizing Data. Hobart Press, Summit, New Jersey. Helsel, D.R., and R.M. Hirsch. (1992). Statistical Methods in Water Resources Research. Elsevier, New York, NY. Hirsch, R.M., D.R. Helsel, T.A. Cohn, and E.J. Gilroy. (1993). Statistical Analysis of Hydrologic Data. In: Maidment, D.R., ed. Handbook of Hydrology. McGraw-Hill, New York, Chapter 17, pp.5--7. Zar, J.H. (2010). Biostatistical Analysis. Fifth Edition. Prentice-Hall, Upper Saddle River, NJ.

See Also

Summary Statistics, summaryFull, var, sd.

Examples

Run this code
# Generate 20 observations from a normal distribution with parameters 
  # mean=10 and sd=2, and compute the standard deviation and 
  # interquartile range. 
  # (Note: the call to set.seed simply allows you to reproduce this example.)

  set.seed(250) 
  dat <- rnorm(20, mean=10, sd=2) 

  sd(dat) 
  #[1] 1.180226
 
  iqr(dat) 
  #[1] 1.489932
 
  #----------

  # Repeat the last example, but add a couple of large "outliers" to the 
  # data.  Note that the estimated standard deviation is greatly affected 
  # by the outliers, while the interquartile range is not.

  summaryStats(dat, quartiles = TRUE) 
  #     N   Mean     SD Median    Min     Max 1st Qu. 3rd Qu.
  #dat 20 9.8612 1.1802 9.6978 7.6042 11.8756  9.1618 10.6517
 
  new.dat <- c(dat, 20, 50) 

  sd(dat) 
  #[1] 1.180226
 
  sd(new.dat) 
  #[1] 8.79796
 
  iqr(dat) 
  #[1] 1.489932
 
  iqr(new.dat) 
  #[1] 1.851472

  #----------
  # Clean up
  rm(dat, new.dat)

Run the code above in your browser using DataLab