[R] Question about functionality of sum()

Vince S. Buffalo vsbuffalo at ucdavis.edu
Tue Jan 23 20:12:52 CET 2007

R community,

My question is a bit complex to explain (and consequently difficult to
search for in the archives). I will illustrate it with a simplified

If I have an array of sample sizes for various factor levels, of all
different sizes (due to missing data), and another array of those same
factor levels' means, and I would like to sum the squares and weight
them with their corresponding sample size as such:

ss <- sum( n*(yi.bar - y..bar)^2)

[note that y..bar is not an array]

Will R parse the array n at the same rate as it is yi.bar (an equally
long array of the means of those n samples)? Note that I am more sure
of using something like:
x <- c(data...)
ss <- sum(length(na.exclude(x))*(mean(na.exclude(x)) - y..bar)^2)

But alas, there are cases when only the means and the sample sizes are
supplied, but not the actual data. I would also like to stray away
from using built-in functions (weighted.mean perhaps?) do gain an
understanding of how this would be done.

Thank you!

"The greatest challenge to any thinker is stating the problem in a way
that will allow a solution."
-Bertrand Russell

More information about the R-help mailing list