[R] Re: [S] tapply for matrices

Robert Keefe keef9490 at uidaho.edu
Fri Oct 11 03:26:53 CEST 2002


Hi All,

On a related note, I am a Newbie, and have written the
following function (below) as a much quicker alternative to
aggregate(). I wrote it because aggregate() seems to bog down
severely on the larger data frames that I work with (let's say
5000 x 50), and when the index variable has many levels, and when
multiple variables are used as indices. My version is just
combining tapply and sapply...

As far as I can tell, the only difference (aside from not
having methods for ts and/or other objects yet), is that my
function doesn't add columns containing the index variable(s)
in the output. (This can easily be added).

Perhaps people can comment on why the approach I've taken
may not be a good one (so that I can improve it, and learn more
about writing functions in general).

Thanks in advance for any suggestions or comments!

Cheers,

Rob


ag.fast <- function(x,by=NULL,FUN=NULL,...) {

 if(is.null(FUN)) stop("You need to specify a legitimate function.")
 FUN <- match.fun(FUN)
 n <- length(by)
 if(n==1)
   int <- by[1]
 else
   int <- interaction(by[1:n], drop=T)

 f <- function(x, FUN,...) {
   tapply(x,int,FUN,...)
  }

 x <- x[,1:ncol(x)]
 s <- sapply(x,f,FUN,...)
 s <- data.frame(cbind(s[,c(1:ncol(s))]))
 return(s)

}


____________

Rob Keefe
M.S. student
Department of Forest Resources
University of Idaho
PO Box 441133
Moscow, ID
83844-1133

Lab: (208) 885-5165
Home: (208) 882-9749




> Frank E Harrell Jr <fharrell at virginia.edu> writes:
>
> > Tony Plate provided what seems to be a very fast and elegant solution - see below.  I have modified his solution slightly:
> >
> > mapply <- function(X, INDEX, FUN=NULL, ..., simplify=TRUE) {
> > ## Matrix tapply
>
> Before this propagates too far: The name "mapply" has also been
> suggested for a "multiple apply", which is like lapply/sapply but
> takes multiple X arguments and a FUN of several arguments. So
> perhaps matrix.apply() or so would be better?
>
> --
>    O__  ---- Peter Dalgaard             Blegdamsvej 3
>   c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
>  (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>






-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list