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