如何在R中创建列表的复杂层次结构

问题描述 投票:1回答:1

我已收到有关如何在列表中创建一组列表的帮助,但是我无法添加另一层/扩展列表的深度。我想要的是在每个列表中添加一个最终的“图层”,即“DataFrame”,“DataFrame2”等等。目前我有:

Layer1 = c('AA', 'BB', 'CC', 'DD')

myList=setNames(as.list(Layer1),Layer1)

myList=lapply(myList, function(x){
  setNames(vector("list",length(Layer1)),paste0(x," vs ",Layer1))
})

产生myListAA,包含BBCCDDAA vs BB,在每个列表中都是另一个列表,例如: AA vs BBBB等,或者在BB vs AA的情况下,里面的列表将读取BB vs BB?? vs ??(以下称为Layer1 = c('AA', 'BB', 'CC', 'DD') Layer3 = c('DataFrame', 'DataFrame2', 'Matrix', 'Matrix2') myList=setNames(as.list(Layer1),Layer1) myList=lapply(myList, function(x){ setNames(vector("list",length(Layer1)),paste0(x," vs ",Layer1)) myList[i]=lapply(myList, function(x){ setNames(vector("list",length(Layer3)),Layer3) }) }) 文件)等等。

所以我认为我可以通过做一些事情来轻松地添加一个额外的层...

myList[i]

我天真地试图使用DataFrame(我知道它不起作用,但我不确定我是否会做任何事情)表明我想要下一层并开始添加空白Matrix?? vs ??向量(进入我的?? vs ??子列表),以便我有'空槽' - 可以这么说 - 将来我的数据移动。

最终我希望每个DataFrame文件夹包含空白DataFrame2MatrixMatrix2lapply

r list dataframe vector lapply
1个回答
1
投票

Layer1循环遍历列表中的每个元素,如结构,并将函数应用于它。值得注意的是,它不包括位置参数。

你想要做的是:

  • 贯穿Layer1的所有元素,并为每个元素创建一个列表,反过来
  • 包含qazxsw poi这些元素中包含的许多元素
  • Layer3中给出的元素一样多

Layer1 <- c('AA', 'BB', 'CC', 'DD')
Layer3 <- c('DataFrame', 'DataFrame2', 'Matrix', 'Matrix2')

my_list <- lapply(Layer1, function(el_layer1_outer) {
   ## create a list with |Layer1| elements
   ## this we do by creating first an inner list vector(.)
   ## and the repeating it |Layer1| times
   ret <- rep(list(setNames(vector("list", length(Layer3)), 
                            Layer3)), 
              length(Layer1))
   setNames(ret, ## ret has no proper names yet
            paste(el_layer1_outer, "vs.", Layer1)) 
})
names(my_list) <- Layer1 ## could have been done with setNames as well
str(my_list)
List of 4
 $ AA:List of 4
  ..$ AA vs. AA:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ AA vs. BB:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ AA vs. CC:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ AA vs. DD:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
 $ BB:List of 4
  ..$ BB vs. AA:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ BB vs. BB:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ BB vs. CC:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ BB vs. DD:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
 $ CC:List of 4
  ..$ CC vs. AA:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ CC vs. BB:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ CC vs. CC:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ CC vs. DD:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
 $ DD:List of 4
  ..$ DD vs. AA:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ DD vs. BB:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ DD vs. CC:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
  ..$ DD vs. DD:List of 4
  .. ..$ DataFrame : NULL
  .. ..$ DataFrame2: NULL
  .. ..$ Matrix    : NULL
  .. ..$ Matrix2   : NULL
© www.soinside.com 2019 - 2024. All rights reserved.