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
David Orme <d.orme at imperial.ac.uk> writes:> 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]]]) > } > } >How about> mysum <- function(x,y)mapply(function(x,y)sum(intersect(x,y)),x,y) > outer(l,l,mysum)[,1] [,2] [,3] [1,] 15 12 0 [2,] 12 42 17 [3,] 0 17 50 (notice that your problem really isn't that you have a list, but that you have functions that don't vectorize over lists.) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
"David Orme" <d.orme at imperial.ac.uk> wrote: ...> 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]]]) > } > } >I'm having a hard time figuring out what you want. Who is this mystery object "test"? When I drop "test" from the text, and use: result[x,y] <- sum(mylist[[x]] %in% mylist[[y]]) It doesn't give a syntax error, but the result is nothing like the one you posted above. 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). Cheers Jason