Skip to contents

Compute weighted quantiles

Usage

weighted_quantile(x, w, probs)

weighted_median(x, w)

Arguments

x

Observations

w

Case weights (optional)

probs

Quantiles to calculate

Value

A vector the same length as probs with the corresponding weighted quantiles of x with weight w. For integer weights, this is equivalent to quantile(rep(x, w), probs)

The weighted median of x with weights w. For integer weights, this is equivalent to median(rep(x, w))

See also

Other weighted statistics: weighted_moments(), weighted_tabulate()

Examples

weighted_median(1:6)
#> [1] 3.5
weighted_median(1:3, c(1, 4, 9))
#> [1] 3
weighted_median(1:3, c(9, 4, 1))
#> [1] 1

weighted_quantile(1:3, c(1, 4, 9), seq(0.0, 1.0, by = 0.25))
#> [1] 1 2 3 3 3
quantile(rep(1:3, c(1, 4, 9)), seq(0.0, 1.0, by = 0.25))
#>   0%  25%  50%  75% 100% 
#>    1    2    3    3    3