[R] Weighted means in aggregate.table

From: smith_cc <smith_cc_at_yahoo.com>
Date: Sun, 09 Mar 2008 12:19:50 -0700 (PDT)

I need to calculate the weighted means by two factors, the subject id and the time of measurement. I was hoping to use aggregate.table, but this function does not allow me to use different weights for each mean that is calculated.

id <- c(rep("a",4),rep("b",4))

time <- c(0,0,60,60,0,0,60,60)
y <- c(10,20,30,40,10,20,30,40)
weight <- c(1,2,3,4,4,3,2,1)

aggregate.table(y,id,time,FUN=weighted.mean,w=weight) Error in FUN(X[[1L]], ...) : 'x' and 'w' must have the same length

The function works if the data for each time are weighted the same across subjects:
aggregate.table(y,id,time,FUN=weighted.mean,w=c(1,2))

   0 60
a 17 37
b 17 37

Can anyone suggest a solution? Thank you in advance!

Sincerely,
Chad Smith

-- 
View this message in context: http://www.nabble.com/Weighted-means-in-aggregate.table-tp15945216p15945216.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sun 09 Mar 2008 - 19:31:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 09 Mar 2008 - 20:30:20 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive