1

I am working with nested lists in R. Basically, I have the lists running through a function to compose all of the sub-lists and I need to collapse them. Suppose I have a list like this

[[1]]
[[1]][[1]]
[1] 2


[[2]]
[[2]][[1]]
[1] 1


[[3]]
[[3]][[1]]
[1] 4

[[3]][[2]]
[1] 22

[[3]][[3]]
[1] 2 2


[[4]]
[[4]][[1]]
[1] 3

[[4]][[2]]
[1] 12

[[4]][[3]]
[1] 1 2

How would I collapse this to one list like so

[[1]]
[1] 2

[[2]]
[1] 1

[[3]]
[1] 4

[[4]]
[1] 22

[[5]]
[1] 2 2

[[6]]
[1] 3

[[7]]
[1] 12

[[8]]
[1] 1 2

I'm sure that it involves some form of lapply and unlist but I can't seem to get it to work...

rdevn00b
  • 552
  • 2
  • 5
  • 14
  • 2
    For this example `unlist(x, recursive = FALSE)` seems to suffice. Is that what you want? – Molx Aug 20 '15 at 03:18
  • I tried that but it didn't work for me :( – rdevn00b Aug 20 '15 at 03:25
  • Why not? It works for the data you posted here. If this isn't like your real data then post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Molx Aug 20 '15 at 03:34
  • I agree with @Moix . It works on the data showed. For example `x <- list(list(2), list(1), list(4, 22, c(2,2)), list(3, 12, c(1,2)))` – akrun Aug 20 '15 at 03:36

0 Answers0