[R] Lists and outer() like functionality?

David Orme d.orme at imperial.ac.uk
Mon May 10 17:42:41 CEST 2004


On 10 May 2004, at 15:45, Jason Turner wrote:

>
> I'm having a hard time figuring out what you want.  Who is this mystery
> object "test"?

Ah - sorry all. That should be:

for(x in ind){
    for(y in ind){
      result[x,y] <- sum(mylist[[x]][mylist[[x]] %in% mylist[[y]]])
    }
}

>
> As a style point, ind <- seq(along=mylist) is a bit more foolproof in 
> the
> case of empty lists being accidentally passed to the code (been bitten
> that way once or twice).
>

Yup - thanks for the hint.

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)?

Thanks,
David



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

 >




More information about the R-help mailing list