3

in my dataset LISTS is a list of lists. Suppose it is composed by 3 lists, each of those made up by 3 matrices. Hence I have 9 matrices in total:

A <- list(matrix(rep(1.1,4),ncol=2), 
          matrix(rep(1.2,4),ncol=2), 
          matrix(rep(1.3,4),ncol=2))

B <- list(matrix(rep(2.1,4),ncol=2),
          matrix(rep(2.2,4),ncol=2), 
          matrix(rep(2.3,4),ncol=2))

C <- list(matrix(rep(3.1,4),ncol=2),
          matrix(rep(3.2,4),ncol=2),
          matrix(rep(3.3,4),ncol=2))

LIST <- list(A,B,C)

What I need to do is to create a new list composed by three lists. The first one made up by the lower-level matrices A[[1]], B[[1]], C[[1]]; the second by A[[2]], B[[2]], C[[2]], and the last one by A[[3]], B[[3]], C[[3]].

At first I tried to isolate single matrices with, for instance, LIST[[1]][[1]] and so on. This works, but then I didn't find the corret syntax for selecting 3 matrices at one time. What I mean (with wrong syntax) is to select the first matrix of each list by using something like LIST[(1:3)][[1]], which is clearly wrong...

In other words, I am wondering whether there is a direct way for selecting a subset of a nested list (similarly to what it is possible to do with matrices or dataframes)

Thank you very much for any help!

4

1 回答 1

1

也许你只是在寻找[[inside lapply,但你的问题不是很清楚。

例如,要从每个子列表中获取第三个元素:

lapply(LIST, `[[`, 3)
# [[1]]
#      [,1] [,2]
# [1,]  1.3  1.3
# [2,]  1.3  1.3
# 
# [[2]]
#      [,1] [,2]
# [1,]  2.3  2.3
# [2,]  2.3  2.3
# 
# [[3]]
#      [,1] [,2]
# [1,]  3.3  3.3
# [2,]  3.3  3.3
于 2013-09-27T17:36:07.193 回答