Re: [R] Arrange a multi-level list to a one-level list

From: Phil Spector <spector_at_stat.berkeley.edu>
Date: Fri, 27 May 2011 15:29:49 -0700 (PDT)

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

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.
>
> ______________________________________________
> R-help_at_r-project.org 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.



R-help_at_r-project.org 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. Received on Fri 27 May 2011 - 22:31:42 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 28 May 2011 - 07:10:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive