4

Or put it differently: How can I use the [[ operator in a nested list?

You can consider this as a follow-up question on this one, when I asked how to determine the depth level of a list. I got some decent answers from @Spacedman and @flodel who both suggested recursive functions. Both solutions where quite similar and worked for me.

However I haven't figured out yet what to do with the information I get from these functions. Let's say I have a list nested at level i and I want to get back a list that contains all i-th level elements, like this:

myList$firstLevel$secondLevel$thirdLevel$fourthLevel
# fourthLevel contains 5 data.frames and thirdLevel has
# three elements

How can I get back all 15 data.frames from mylist? I am trying to use e.g.

lapply(mylist,"[[",2)

but obviously I just get the second element of all list elements at the first level.

EDIT: I found the following in the help of extract respectivel ?"[[" but can't really wrap my head around it so far: "[[ can be applied recursively to lists, so that if the single index i is a vector of length p, alist[[i]] is equivalent to alist[[i1]]...[[ip]] providing all but the final indexing results in a list."

EDIT: Don't want to end up nesting loops like this.

o <- list()
i=1
for (i in 1:2){
o[[i]] <- mylist[[c(i,1,1,1)]]
} 
Community
  • 1
  • 1
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
  • 4
    it means `mylist[[ c(1,2) ]]` will get you the second element of the first element of `mylist`. – Romain Francois Nov 19 '12 at 11:09
  • Thanks Romain, actually this helps as I could try to determine max values for very level and then loop through them. So the next step would be: How to find length of my list recursively? – Matt Bannert Nov 19 '12 at 11:20
  • Are you asking for the length at each level, or just the lengths of the lowest-level list elements? – Carl Witthoft Nov 19 '12 at 12:33
  • sorry for being imprecise. I mean the length at each level. – Matt Bannert Nov 19 '12 at 13:17
  • If at level 4 you had the 15 data.frames you mention plus a nested list, would you like to return just the 15 data.frames or everything all together? In other words, do you only want the leaves at a specific level, or both leaves and branches? – flodel Nov 19 '12 at 14:08
  • @flodel: Basically I am trying to understand the whole thing and get a better understanding of recursive functions (in R). In this specific situation I have a moderate number of elements (1-3) on the first levels and 5+ on the last level (which are all data.frames). All I want is to put all of these data.frames into a 1 level named list, which I can bring to an environment later on using list2env at the very end. – Matt Bannert Nov 19 '12 at 14:19
  • I guess at the moment it would simply help a lot if I knew how to refer to the deepest level when I determined the depth with the help of the function you shown yesterday. – Matt Bannert Nov 19 '12 at 14:55

1 Answers1

-1

I found the answer in the meantime. Can't say say I did it on my own. This link gives an elaborate explanation how to use another (complex) recursive function to linearize the whole nested list wad.

What's really nice about the solution provided by Akhil S. Behl: it deals with the fact that data.frames are lists too and recursing can stop before data.frames. It turned out that this was one of my major problems before.

Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
  • 1
    I was looking for a solution to a similar problem and this question sounded promising. Sadly, the link to the solution no longer works. This is why SO discourages use of external links. – arielf Sep 26 '15 at 22:38
  • @arielf I am sorry you are right about external link. Guess that was a rookie's mistake. It's been a while but I think I used akhil's code in an old project https://github.com/mbannert/gateveys2 . If so I gave him credit in the comments and his name should be easy to find. Hth – Matt Bannert Sep 26 '15 at 22:47