Description Usage Arguments Value Examples
This is a faster version of base R function
weighted.mean
. For optimal speed, specify
x.integer = TRUE
and w.integer = TRUE
if x
and w
are integer vectors.
1 | weighted.mean2(x, w, x.integer = FALSE, w.integer = FALSE)
|
x |
Integer or numeric vector. |
w |
Integer or numeric vector of weights. |
x.integer |
Logical value for whether |
w.integer |
Logical value for whether |
Numeric value.
1 2 3 4 5 6 7 8 9 10 11 12 13 | # weighted.mean2 vs. weighted.mean for integer vectors
x <- rpois(1000, lambda = 5)
w <- rpois(1000, lambda = 1)
all.equal(weighted.mean(x, w), weighted.mean2(x, w, TRUE, TRUE))
benchmark(weighted.mean(x, w), weighted.mean2(x, w, TRUE, TRUE),
replications = 1000)
# weighted.mean2 vs. weighted.mean for numeric vectors
x <- rnorm(1000)
w <- rexp(1000)
all.equal(weighted.mean(x, w), weighted.mean2(x, w))
benchmark(weighted.mean(x), weighted.mean2(x, w),
replications = 1000)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.