[R] List subsetting

Ivo Shterev idc318 at yahoo.com
Thu Jun 25 01:42:01 CEST 2009


Hello,

I have a question about list indexing. Lets say we have a list of 3 lists, each containing 3 different type elements:

> a=replicate(3, list(list(c(1,1,1), diag(3), c(2,2,2))))
> a
[[1]]
[[1]][[1]]
[1] 1 1 1

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

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


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

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

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


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

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

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


If anyone can point a direction as to how to obtain (subset) the following list from list a:
> b
[[1]]
[[1]][[1]]
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1

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


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

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

Also, suppose that one wishes to assign the list "b" to the corresponding subset of list "a". Is there a way of doing so? 

Thanks for the help.
Ivo




More information about the R-help mailing list