Skip to content
Prev 69753 / 398525 Next

Not sure if this is "aggregate" or some other task.

On 5/15/05, David L. Van Brunt, Ph.D. <dlvanbrunt at gmail.com> wrote:
This seems like a situation where you want to process the sub-data.frame
corresponding to each Subject.  'by' will do that.    If 'z' is your data.frame
then 'z.by' is an object of class "by" which has the desired result and
the last line converts that to a data.frame:


f <- function(x) {
      y <- colMeans(x[1:2,])
      y[2] <- 1
     rbind(y, x[2,])
}
z.by <- by(z, list(Subj = z$Subj), f)
do.call(rbind, z.by)