[R] Lists and outer() like functionality?

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


Hi,

I'm have a list of integer vectors and I want to perform an outer() 
like operation on the list. As an example, take the following list:

mylist <- list(1:5,3:9,8:12)

A simple example of the kind of thing I want to do is to find the sum 
of the shared numbers between each vector to give a result like:

result <- array(c(15,12,0,12,42,17,0,17,50), dim=c(3,3))

Two for() loops is the easiest way but I wondered if there was a 
neater/faster solution.

mylist.len <- length(mylist)
ind <- 1:mylist.len
result <- array(NA, dim=c(mylist.len,mylist.len))
for(x in ind){
   for(y in ind){
     result[x,y] <- sum(mylist[[x]][test[[x]] %in% test[[y]]])
   }
}


Many thanks,
David Orme




More information about the R-help mailing list