Skip to contents

Calculates empirical sample quantiles with optional weights, for given probabilities. Like in quantile(), the smallest observation corresponds to a probability of 0 and the largest to a probability of 1. Interpolation between discrete values is done when type=7, as in quantile(). Use type=1 to only generate quantile values from the raw input samples.

Usage

wquantile(
  x,
  probs = seq(0, 1, 0.25),
  na.rm = FALSE,
  type = 7,
  weights = NULL,
  ...
)

Arguments

x

numeric vector whose sample quantiles are wanted. NA and NaN values are not allowed in numeric vectors unless na.rm is TRUE.

probs

numeric vector of probabilities with values in \([0,1]\).

na.rm

logical; if true, any NA and NaN's are removed from x before the quantiles are computed.

type

numeric, 1 for no interpolation, or 7, for interpolated quantiles. Default is 7.

weights

numeric vector of non-negative weights, the same length as x, or NULL. The weights are normalised to sum to 1. If NULL, then wquantile(x) behaves the same as quantile(x), with equal weight for each sample value.

...

Additional arguments, currently ignored

See also

stat_ewcdf

Examples

# Some random numbers
x <- rnorm(100)

# Plain quantiles:
quantile(x)
#>         0%        25%        50%        75%       100% 
#> -2.5756814 -0.6702254 -0.0456735  0.9926358  3.2920508 

# Larger values given larger weight, on average shifting the quantiles upward:
wquantile(x, weights = sort(runif(length(x))))
#> [1] -2.5756814 -0.6882400 -0.2038657  0.8700143  3.2920508