[R] Arrange a multi-level list to a one-level list
Phil Spector
spector at stat.berkeley.edu
Sat May 28 00:29:49 CEST 2011
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
spector at stat.berkeley.edu
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.
More information about the R-help
mailing list