weighted.mean2: Weighted Arithmetic Mean

Description Usage Arguments Value Examples

Description

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.

Usage

1
weighted.mean2(x, w, x.integer = FALSE, w.integer = FALSE)

Arguments

x

Integer or numeric vector.

w

Integer or numeric vector of weights.

x.integer

Logical value for whether x is an integer vector.

w.integer

Logical value for whether w is an integer vector.

Value

Numeric value.

Examples

 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)

vandomed/crowdopt documentation built on May 26, 2019, 5:35 a.m.