0

I have 10 lists named : Thre1, Thre2, Thre3, ..., Thre10.

My output should also be a list in a way that ith element of the output should be intersect of ith elements of my input lists so for example:

output[[1]] = Reduce(intersect, 
                     list=(Thre1[[1]],Thre2[[1]],Thre3[[1]],...,Thre10[[1]])

How can I write the code to prevent repeating typing Threi[[j]] by hand?

Andrie
  • 176,377
  • 47
  • 447
  • 496
hora
  • 845
  • 5
  • 14
  • 25

1 Answers1

1

Assuming all lists have the same length:

lapply(1:length(Thre1), function(i)
   Reduce(intersect, list(Thre1[[i]], ..., Thre10[[i]])))
Ernest A
  • 7,526
  • 8
  • 34
  • 40
  • @hora sorry my first answer was wrong, now I think it's correct. – Ernest A Nov 15 '12 at 14:22
  • And how about in the list(Thre1[[i]],...,Thre10[[i]]) ? should I repeat the list names or is it a way to produce them automatically? because I have so many lists also with other names like Rank1 ... Rank10 and so on. – hora Nov 15 '12 at 14:27
  • 2
    The first thing to do is to change your 10 lists to list: Thre = list(Thre1,Thre2,Thre3,Thre4,Thre5,Thre6,Thre7,Thre8,Thre9,Thre10) This will make your life easier! – Ali Nov 15 '12 at 14:34
  • 1
    You can follow @Ali's advice by doing `myList <- lapply(paste0("Thre",1:10),get)` but you will be better off in the long run restructuring code to define these as list elements in the first place – Ben Bolker Nov 15 '12 at 14:43
  • The problem is that right now I have so many nested lists and I prefer not to make more variables but producing them automatically. – hora Nov 15 '12 at 14:44
  • @Ali , I did your suggestion but again I need to write: output[[1]] = Reduce(intersect,list(Thre[[1]][[1]],Thre[[2]][[1]],Thre[[3]][[1]],...,Thre[[10]][[1]]) dont I? Now I am confused. Can you reproduce the intersect code depends on your suggestion? Thanks in advance – hora Nov 15 '12 at 15:17