
Compute the interquartile range for a set of data.
iqr(x, na.rm = FALSE)
numeric vector of observations.
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
,
missing values are removed from x
prior to computing the coefficient
of variation.
A numeric scalar -- the interquartile range.
Let
See the R help file for quantile
for information on how sample
quantiles are computed.
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.
# NOT RUN {
# 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