For each level of factor aggregate values over all levels except the current one (in R)

Posted by Andrey Chetverikov on Stack Overflow See other posts from Stack Overflow or by Andrey Chetverikov
Published on 2012-09-30T09:35:18Z Indexed on 2012/09/30 9:37 UTC
Read the original article Hit count: 147

Filed under:
|

For each level of factor I need to extract values aggregated over all subsets of data.frame except the current one. For example, there is a several subjects doing a reaction time task during several days, and I need to compute mean reaction time for all subjects and all days, but not including the subject for whom the mean is computed. Currently, I do it like this:

 library(lme4)
 ddply(sleepstudy, .(Subject, Days), summarise , avg_rt=mean(sleepstudy[sleepstudy$Subject!=Subject&sleepstudy$Days==Days,"Reaction"]), .progress="text")

It works fine for small data sets, but for large ones it can be very slow. Is there a way to do it faster?

© Stack Overflow or respective owner

Related posts about r

    Related posts about aggregate