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@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.