Arrange a multi-level list to a one-level list

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Arrange a multi-level list to a one-level list

Lisa
Dear all,

I just want to arrange a multi-level list to a one-level list. For example:

x <- list(list(matrix(sample(c(0,1), 4, replace = T), 2), matrix(sample(c(2,3), 4, replace = T), 2)),
list(matrix(sample(c(0,1), 6, replace = T), 2), matrix(sample(c(2,3), 6, replace = T), 2)))

> x
[[1]]
[[1]][[1]]
     [,1] [,2]
[1,]    1    1
[2,]    0    0

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


[[2]]
[[2]][[1]]
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0

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

If I do this work one time, I will arrange it like this:
 
c(x[[1]], x[[2]]) # Only consider the first level of the list.

[[1]]
     [,1] [,2]
[1,]    1    1
[2,]    0    0

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

[[3]]
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0

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

But when I repeat this work many times and the levels will change each time in a loop, how can I pass the arguments to c()? For example, in the first iteration, the first level of a list is 3, in the second iteration, the first level of a list is 5? ….

Any help would be greatly appreciated.

Lisa
Reply | Threaded
Open this post in threaded view
|

Re: Arrange a multi-level list to a one-level list

Phil Spector
Lisa -
    Are you looking for this?

> x <- list(list(matrix(sample(c(0,1), 4, replace = T), 2),
+ matrix(sample(c(2,3), 4, replace = T), 2)),
+ list(matrix(sample(c(0,1), 6, replace = T), 2), matrix(sample(c(2,3), 6,
+ replace = T), 2)))
> unlist(x,recursive=FALSE)
[[1]]
      [,1] [,2]
[1,]    0    0
[2,]    1    0

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

[[3]]
      [,1] [,2] [,3]
[1,]    1    0    1
[2,]    0    1    0

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

  - Phil Spector
  Statistical Computing Facility
  Department of Statistics
  UC Berkeley
  [hidden email]


On Fri, 27 May 2011, Lisa wrote:

> Dear all,
>
> I just want to arrange a multi-level list to a one-level list. For example:
>
> x <- list(list(matrix(sample(c(0,1), 4, replace = T), 2),
> matrix(sample(c(2,3), 4, replace = T), 2)),
> list(matrix(sample(c(0,1), 6, replace = T), 2), matrix(sample(c(2,3), 6,
> replace = T), 2)))
>
>> x
> [[1]]
> [[1]][[1]]
>     [,1] [,2]
> [1,]    1    1
> [2,]    0    0
>
> [[1]][[2]]
>     [,1] [,2]
> [1,]    3    2
> [2,]    3    3
>
>
> [[2]]
> [[2]][[1]]
>     [,1] [,2] [,3]
> [1,]    1    0    0
> [2,]    0    1    0
>
> [[2]][[2]]
>     [,1] [,2] [,3]
> [1,]    2    3    3
> [2,]    3    3    3
>
> If I do this work one time, I will arrange it like this:
>
> c(x[[1]], x[[2]]) # Only consider the first level of the list.
>
> [[1]]
>     [,1] [,2]
> [1,]    1    1
> [2,]    0    0
>
> [[2]]
>     [,1] [,2]
> [1,]    3    2
> [2,]    3    3
>
> [[3]]
>     [,1] [,2] [,3]
> [1,]    1    0    0
> [2,]    0    1    0
>
> [[4]]
>     [,1] [,2] [,3]
> [1,]    2    3    3
> [2,]    3    3    3
>
> But when I repeat this work many times and the levels will change each time
> in a loop, how can I pass the arguments to c()? For example, in the first
> iteration, the first level of a list is 3, in the second iteration, the
> first level of a list is 5? ….
>
> Any help would be greatly appreciated.
>
> Lisa
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Arrange-a-multi-level-list-to-a-one-level-list-tp3556500p3556500.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Arrange a multi-level list to a one-level list

Lisa
Hi, Phil,

Yes. That's what I am looking for. Thank you so much.

Lisa