[R] Lists and outer() like functionality?

David Orme d.orme at imperial.ac.uk
Mon May 10 18:16:16 CEST 2004


>
> outer() generally works by expanding out the lists like
>
> Y <- rep(y,length(x))
> X <- rep(x,each=length(y)) # or maybe it's vice versa, never mind...
> FUN(X,Y,extras)
>
> and then adds dims and dimnames. If FUN vectorizes, this is the
> efficient way, but it does not in your case and the rep()s are
> probably not cheap when lists are involved.
>
> you might try this sort of stuff:
>
> myfunc2 <- function(i,j)
>     mapply(function(i,j)
>              
> phylo.overlap(assemblages[[i]],assemblages[[j]],parrot.cm),
>            i,j)
> ind <- seq(along=assemblages)
> outer(ind,ind,myfunc2)
>

That now runs at about the same speed as the loops (although the loops 
still just have the edge).

Many thanks for the suggestions.

David




More information about the R-help mailing list