weighted.var: Compute a weighted variance or standard deviation of a...

Description Usage Arguments Details See Also Examples

View source: R/weighted-stats.r

Description

Compute a weighted variance or standard deviation of a vector.

Usage

1
2
3
weighted.var(x, w = NULL, na.rm = FALSE)

weighted.sd(x, w, na.rm = TRUE)

Arguments

x

numeric vector of observations

w

integer vector of weights, representing the number of time each x was observed

na.rm

if TRUE, missing values in both w and x will be removed prior computation. Otherwise if there are missing values the result will always be missing.

Details

Note that unlike the base R var function, these functions only work with individual vectors not matrices or data frames.

See Also

weighted.mean

Other weighted statistics: weighted.ecdf; weighted.quantile

Examples

1
2
3
4
5
6
7
x <- c(1:5)
w <- rpois(5, 5) + 1
y <- x[rep(seq_along(x), w)]
weighted.var(x, w)
var(y)

stopifnot(all.equal(weighted.var(x, w), var(y)))

hadley/bigvis documentation built on May 17, 2019, 9:45 a.m.