[R] Aggregate with numerous factors

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Mon Dec 18 12:11:52 CET 2006


Peter Dalgaard wrote:
> Alternatively, rewrite aggregate() and send us a patch ;-)
>
> It is not necessarily all that hard. Here's a rough idea
>
> IX <- as.data.frame(by)
> OO <- do.call(order,IX)
> Y <- x[OO,]
> g <- cumsum(!duplicated(IX))
> FF <- unique(IX)
> cbind(FF, sapply(split(x,g),FUN))
>
> (completely untested, of course, and if it works, it works only for a
> single-column x; otherwise, you need a loop over the columns somehow.
>   
I see two glaring blunders already...

You need IX[OO,] in two places, and split(Y, g) not x

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list