0

I have a very messy list with multiple levels in the form of:

[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[1] "D" "B" "A"

[[1]][[1]][[2]]
[1] "E" "B" "A"


[[1]][[2]]
[[1]][[2]][[1]]
[1] "D" "C" "A"


[[1]][[3]]
[[1]][[3]][[1]]
[1] "B" "D" "A"

....

[[5]][[2]][[2]]
[1] "D" "B" "E"


[[5]][[3]]
[1] "C" "E"

...

What is the easiest way to just get a list of the lowest level character vectors, so the first element would be "D""B""A" then the next would be "E""B""A" and so forth?

Thanks!

Edit: Here's my list in dput format as requested. However, the nesting structure can change and the number of levels can increase. Thus any solution that works by using a presupposed number of levels is no good.

> dput(myResults)
list(list(list(c("D", "B", "A"), c("E", "B", "A")), list(c("D", 
"C", "A")), list(c("B", "D", "A"), c("C", "D", "A"), c("E", "D", 
"A")), list(c("B", "E", "A"), c("D", "E", "A"))), list(list(c("D", 
"A", "B"), c("E", "A", "B")), c("C", "B"), list(c("A", "D", "B"
), c("E", "D", "B")), list(c("A", "E", "B"), c("D", "E", "B"))), 
    list(list(c("D", "A", "C")), c("B", "C"), list(c("A", "D", 
    "C")), c("E", "C")), list(list(c("B", "A", "D"), c("C", "A", 
    "D"), c("E", "A", "D")), list(c("A", "B", "D"), c("E", "B", 
    "D")), list(c("A", "C", "D")), list(c("A", "E", "D"), c("B", 
    "E", "D"))), list(list(c("B", "A", "E"), c("D", "A", "E")), 
        list(c("A", "B", "E"), c("D", "B", "E")), c("C", "E"), 
        list(c("A", "D", "E"), c("B", "D", "E"))))
user1357015
  • 11,168
  • 22
  • 66
  • 111
  • 2
    could you please provide a `dput` of your data or a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? subsetting nested lists sometimes is very tricky, it hard to help you without a piece of code. A visual representation of the desired output could be very useful as well. – SabDeM Jul 31 '15 at 01:23
  • 2
    It might be easier to modify the code that produces this list. – Matthew Lundberg Jul 31 '15 at 01:35
  • problem is that I don't see it -- it's passed down to me... hmm your point is well taken though. I'll follow back... I thought there must be a function that gets to the base level and appends it – user1357015 Jul 31 '15 at 01:36
  • 1
    I was going to suggest `u <- unlist(x); l <- rapply(x, length)` and then split `u` based on the lengths in `l`. But now it's a duplicate. Probably not efficient but it seems easier to accomplish and is probably safe. – Rich Scriven Jul 31 '15 at 02:32

2 Answers2

1

Edit

There is a package rlist with a function list.flatten that does this

library(rlist)
list.flatten(yourLst)

A recursive solution (the order is changed though, ie. the leastly nested stuff comes out first)

unlst <- function(lst){
    if (!any((inds <- sapply(lst, is.list)))) return(lst)
    c(lst[!inds], unlst(unlist(lst[inds], rec=F)))
}
Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • `rlist::list.flatten` is nearly a verbatim copy of `flatten2` in the [currently accepted answer](http://stackoverflow.com/a/8139959/271616) to the question this duplicates. – Joshua Ulrich Jul 31 '15 at 03:02
  • 1
    Thanks for reminding me the copyright problem, the author deserves full credit of the implementation of `rlist::list.flatten`. Just pushed the documentation that includes the attribution. – Kun Ren Jul 31 '15 at 04:37
1

Try this function please.

unlist_messy_list <- function(cur_list){
    if (is.atomic(cur_list)){
        list(cur_list)
    }else{
        cl <- lapply(cur_list, unlist_messy_list)
        Reduce(c, cl)
    }
}

As you have not provided a sample data , I tested it with some cases made up by myself and it works.

unlist_messy_list(list())
unlist_messy_list(list(c(1,2,3), c(4,5,6), c(7,8,9)))
unlist_messy_list(list(c(1,2,3), list(c(4,5,6), c(7,8,9))))
unlist_messy_list(list(c(1,2,3), c(4,5,6), list(c(7,8,9), c(10,11,12))))
unlist_messy_list(list(c(1,2,3), list(c(4,5,6), c(7,8,9), list(10, c(11,12,13), 14, list(c(15,16))))))

I just tested it on your newly provided data, and it works fine. The output is (after dput):

list(c("D", "B", "A"), c("E", "B", "A"), c("D", "C", "A"), c("B", "D", "A"), c("C", "D", "A"), c("E", "D", "A"), c("B", "E", "A"), c("D", "E", "A"), c("D", "A", "B"), c("E", "A", "B"), c("C", "B"), c("A", "D", "B"), c("E", "D", "B"), c("A", "E", "B"), c("D", "E", "B"), c("D", "A", "C"), c("B", "C"), c("A", "D", "C"), c("E", "C"), c("B", "A", "D"), c("C", "A", "D"), c("E", "A", "D"), c("A", "B", "D"), c("E", "B", "D"), c("A", "C", "D"), c("A", "E", "D"), c("B", "E", "D"),c("B", "A", "E"), c("D", "A", "E"), c("A", "B", "E"), c("D", "B", "E"), c("C", "E"), c("A", "D", "E"), c("B", "D", "E"))
Ping Jin
  • 520
  • 4
  • 8