[R] weighted.mean and tapply (again)

james.holtman@convergys.com james.holtman at convergys.com
Wed May 25 17:56:37 CEST 2005





> x.1 <- read.table('clipboard',header=T)
> x.1
   GROUP VALUE FREQUENCY
1      2     2        78
2      2     3        40
3      2     4        16
4      2     5         3
5      2     6         1
6      2     8         1
7      3     3        19
8      3     4        10
9      3     5        19
10     3     6         4
> by(x.1, x.1$GROUP, function(x) weighted.mean(x$VALUE, x$FREQUENCY))
x.1$GROUP: 2
[1] 2.654676
---------------------------------------------------------------------------
x.1$GROUP: 3
[1] 4.153846
>

Jim
__________________________________________________________
James Holtman        "What is the problem you are trying to solve?"
Executive Technical Consultant  --  Office of Technology, Convergys
james.holtman at convergys.com
+1 (513) 723-2929


                                                                                                                                           
                      Dan Bolser                                                                                                           
                      <dmb at mrc-dunn.cam.ac.        To:       R mailing list <r-help at stat.math.ethz.ch>                                     
                      uk>                          cc:                                                                                     
                      Sent by:                     Subject:  [R] weighted.mean and tapply (again)                                          
                      r-help-bounces at stat.m                                                                                                
                      ath.ethz.ch                                                                                                          
                                                                                                                                           
                                                                                                                                           
                      05/25/2005 11:33                                                                                                     
                                                                                                                                           





I read answers to questions including the words "tapply" and
"weighted.mean", but I didn't understand either the problem (data) or the
solution provided.

Here is my question ...

> dat[1:10,]
  GROUP  VALUE FREQUENCY
1     2      2        78
2     2      3        40
3     2      4        16
4     2      5         3
5     2      6         1
6     2      8         1
7     3      3        19
8     3      4        10
9     3      5        19
1     3      6         4


For each GROUP, I would like to calculate the weighted.mean of VALUE using
the FREQUENCY as the weight, so for the snippet of data shown that would
be...

group.2 <- weighted.mean(c(2,3,4,5,6,8),c(78,40,16,3,1,1))
group.3 <- weighted.mean(c(3,4,5,6),    c(19,10,19,4))

> cbind(rbind(2,3),rbind(group.2,group.3))
        [,1]     [,2]
group.2    2 2.654676
group.3    3 4.153846

I would like to use tapply to automatically do this across the whole
dataset (dat) - which includes lots of other distinct grouping factors,
however, like I said, I couldn't understand (and therefore apply to my
data) any of the other solutions I found, so any help here would be
greatly appreciated!

All the best,
Dan.

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list