wtd_mean | R Documentation |
This is an improvement on weighted.mean
in base-r
.
wtd_mean(x, w = NULL, error = F)
x |
(num vector) A vector of values. |
w |
(num vector) A vector of weights. |
error |
(lgl scalr) Whether to throw an error if there is no data at all or no pairwise complete cases. Default yes. |
The original function returns NA
when there are missing values in the weights vector despite na.rm=T. This function avoids that problem. It also returns a useful error message if there are no complete cases. The function wraps base-r's function.
set.seed(1)
X = rnorm(100)
set.seed(1)
W = runif(100)
wtd_mean(X) # not using weights
mean(X) #same as above
wtd_mean(X, W) #slightly different
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.