How to compute weighted mean in R?

mean, r, weighted-average

Solution

Use `weighted.mean`:

> weighted.mean(z$size, z$count)
[1] 4

Problem

How do I compute the weighted mean in `R`? For example, I have 4 elements of which 1 element is of size (or: length, width, etc.) 10 and 3 elements are of size 2. ``` > z = data.frame(count=c(1,3), size=c(10,2)) > z count size 1 1 10 2 3 2 ``` The weighted average is `(10 * 1 + 2 * 3) / 4 = 4`.

Original source

Related problems