Seems like you already know how to calculate this, just need a nudge in the right direction to implement it. Since R is vectorized, this is pretty simple:
with(z, sum(count*size)/sum(count))
The with
bit just saves on typing and is equivalent to sum(z$count*z$size)/sum(z$count)
Or use the built in function weighted.mean()
as you also pointed out. Using your own function can prove faster, though will not do the same amount of error checking that the builtin function does.
builtin <- function() with(z, weighted.mean(count, size))
rollyourown <- function() with(z, sum(count*size)/sum(count))
require(rbenchmark)
benchmark(builtin(), rollyourown(),
replications = 1000000,
columns = c("test", "elapsed", "relative"),
order = "relative")
#-----
test elapsed relative
2 rollyourown() 13.26 1.000000
1 builtin() 22.84 1.722474