[R] Lists and outer() like functionality?

Gabor Grothendieck ggrothendieck at myway.com
Mon May 10 21:00:07 CEST 2004


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

: 
: >
: > 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


Actually if you are debating between for and outer, note that 
one could implement this without for and without outer:

sapply(mylist,function(x)sapply(mylist,function(y)sum(intersect(x,y)))




More information about the R-help mailing list