[R] a weighted average by group

Weiwei Shi helprhelp at gmail.com
Fri Nov 18 00:25:45 CET 2005


Hi,
I have a data.frame a like this:
> a
   v1 v2 v3 v4
1   1  1  A  X
2   2  2  A  X
3   3  3  A  X
4   4  4  B  X
5   5  5  B  X
6   6  6  C  Y
7   7  7  C  Y
8   8  8  C  Y
9   9  9  D  Y
10 10 10  D  Y

I want to get a weighted average for each combination of v3 and v4,
using v1 as values and v2 as weights i.e.,
for A and X, the final result should be (1*1+2*2+3*3)/(1+2+3)

not sure which function I should use in R ?

thanks,
--
Weiwei Shi, Ph.D

"Did you always know?"
"No, I did not. But I believed..."
---Matrix III




More information about the R-help mailing list