varW | R Documentation |
Calculate the weighted variance of x
.
varW(x, w = NULL, na.rm = FALSE)
x |
A numeric vector. |
w |
A numeric vector of weights of the same length as |
na.rm |
Logical, whether NAs in |
Calculate the weighted variance of x
via the weighted covariance
matrix (cov.wt()
). If no weights are supplied, the simple variance is
returned instead. As in weighted.mean()
, NA
s in w
are not handled
specially and will return NA
as result.
A numeric value, the weighted variance of x
.
var()
# Weighted variance
x <- rnorm(30)
w <- runif(30, 0, 1)
varW(x, w)
# Simple variance
varW(x)
stopifnot(varW(x) == var(x))
# NA handling
varW(c(x[1:29], NA), w, na.rm = TRUE) # NA in x (removed)
varW(c(x[1:29], NA), w, na.rm = FALSE) # NA in x (NA returned)
varW(x[1:29], w = c(w[1:29], NA)) # NA in w (NA returned)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.