apply outer() on two lists

outer-join, r

Solution

How about the following:

exm = list( elm1=c('a', 'b'), elm2=c('b', 'c', 'd'), elm3=c('b', 'c', 'd', 'e'))
#Use mapply to vectorise your function
int2 <- function(x,y) mapply(function(x,y) {length( intersect( x, y ) )},
                             exm[x], exm[y])

#Use outer on the indices of exm, rather than exm itself
s <- seq_along(exm)
outer(s,s,int2)

#      [,1] [,2] [,3]
# [1,]    2    1    1
# [2,]    1    3    3
# [3,]    1    3    4

Problem

I have a list, say `exm = list( elm1=c('a', 'b'), elm2=c('b', 'c', 'd'), elm3=c('b', 'c', 'd', 'e'))`. I want to apply a function on every combination of two elements from `exm`, e.g., `length( intersect( exm$elm1, exm$elm2 ) )`. The result should be a symmetric matrix. The function `outer` seems to do this job, but it works only for vector, not list. Any idea to do this?

Original source