# generate data and weights
set.seed(1)
x <- rnorm(10)
w <- rpois(10, lambda = 10)
# weighted mean
xbar <- wtd.mean(x, w)
# weighted variance
wtd.var(x, w)
(10 / 9) * sum(w * (x - xbar)^2) / sum(w)
# weighted standard deviation
wtd.sd(x, w)
sqrt((10 / 9) * sum(w * (x - xbar)^2) / sum(w))
Run the code above in your browser using DataLab