# Re: [R] Lists and outer() like functionality?

From: Peter Dalgaard (p.dalgaard@biostat.ku.dk)
Date: Tue 11 May 2004 - 01:56:10 EST

```Message-id: <x2ad0gqns5.fsf@biostat.ku.dk>

```

David Orme <d.orme@imperial.ac.uk> writes:

> Following up on Peter Dalgaard's suggestion, I've tried vectorizing
> the function properly. Now I get the same answers from both the two
> for() loops and the vectorized outer() call. What puzzles me slightly
> is that using outer is about 13 times slower. I realize that
> loops aren't always bad but it seems odd that this should be so much
> slower. Have I got something wrong (the function phylo.overlap
> probably isn't optimal but this should effect relative timings)?

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)

> myfunc <- function(x,y){mapply
> function(x,y){phylo.overlap(x,y,parrot.cm)},x,y)}
> > system.time(outer.test <- outer(assemblages, assemblages, myfunc))
> [1] 62.99 0.25 63.35 0.00 0.00
> > x[1:10,1:5]
> 19916 19917 20275 20992 22787 23008 23009 23145 23146 23147
> 19916 17466 16443 17397 14368 12687 0 0 8396 9843 12687
> 19917 16443 17032 16374 14368 12687 0 0 8396 9843 12687
> 20275 17397 16374 18420 15391 13710 0 0 8396 10866 13710
> 20992 14368 14368 15391 19735 17812 0 0 10452 14968 17812
> 22787 12687 12687 13710 17812 19908 0 0 12548 17064 19908
>
> > assemblage.len <- length(assemblages)
> > ind <- seq(along=assemblages)
> > loop.test <- array(dim=c(assemblage.len,assemblage.len))
> >
> > system.time(
> + for(x in ind){
> + if(x %% 100 == 0) cat(x,"\n")
> + for(y in ind){
> + loop.test[x,y] <- phylo.overlap(assemblages[[x]],
> assemblages[[y]], parrot.cm)
> + }
> + }
> + )
> [1] 4.34 0.17 4.52 0.00 0.00
> > result[1:10,1:5]
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,] 17466 16443 17397 14368 12687 0 0 8396 9843 12687
> [2,] 16443 17032 16374 14368 12687 0 0 8396 9843 12687
> [3,] 17397 16374 18420 15391 13710 0 0 8396 10866 13710
> [4,] 14368 14368 15391 19735 17812 0 0 10452 14968 17812
> [5,] 12687 12687 13710 17812 19908 0 0 12548 17064 19908
>
> >
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>

```--
O__  ---- Peter Dalgaard             Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
(*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help