Compute the interquartile range of x, with optional weights.
Arguments
- x
numeric vector
- weights
optional numeric vector of non-negative sample weights, of the same length as
x- na.rm
logical; whether to remove missing values. If
FALSEandxcontains missing values, the result isNA.- type
integer selecting the quantile algorithm, see
quantileX(). The defaultNULLpicks the one that suits the branch taken:7without weights, as instats::IQR();2with weights, which reads them as relative weights and depends only on their ratios. Passtype = 7explicitly to read the weights as replication counts.
Details
The IQR is the difference of the 0.75 and the 0.25 quantile as computed
by quantileX(). Without weights the result is identical to
stats::IQR(), except that a missing value yields NA instead of an
error when na.rm = FALSE.
See also
medianX(), quantileX(), stats::IQR(), stats::quantile()
Other dispersion:
coefVar(),
madX(),
meanAbsDev(),
meanSE(),
rangeX(),
varX()
Examples
x <- c(3.7, 3.3, 3.5, 2.8)
w <- c(5, 5, 4, 1) / 15
iqrX(x)
#> [1] 0.375
iqrX(x, weights = w)
#> [1] 0.4
iqrX(x, weights = w * 15) # same: only the ratios count
#> [1] 0.4
iqrX(x, weights = w * 15, type = 7) # replication counts
#> [1] 0.4
iqrX(c(x, NA)) # NA, no error
#> [1] NA
iqrX(c(x, NA), na.rm = TRUE)
#> [1] 0.375
