【问题标题】:How to create complex hierarchies of lists in R如何在 R 中创建复杂的列表层次结构
【发布时间】:2019-04-12 07:20:59
【问题描述】:

我收到了有关如何在列表中创建一组列表的帮助,但是我无法添加另一个层/扩展列表的深度。我想要的只是在每个列表中添加一个最终的“层”,即“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))
})

产生myList,包含AABBCCDD,在每个列表中都有一个进一步的列表,例如AA vs BBAA vs BB 等,或者在BB 的情况下,里面的列表将读取BB vs AABB 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](我知道这不起作用,但我不确定我正在做的任何事情是否会)表明我想降级并开始添加空白的DataFrameMatrix 向量(到我的?? vs ?? 子列表中),这样我就有了“空槽”——可以这么说——将来可以将我的数据移动到其中。

最终我希望每个?? vs ?? 文件夹都包含一个空白DataFrameDataFrame2MatrixMatrix2

【问题讨论】:

    标签: r list dataframe vector lapply


    【解决方案1】:

    lapply 循环遍历列表状结构的每个元素并对其应用函数。值得注意的是,它包含位置参数。

    你想做的是:

    • 遍历Layer1 的所有元素并为每个元素创建一个列表,依次进行
    • 包含Layer1许多元素,其中这些元素包含
    • 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
    

    【讨论】:

    • 谢谢,这真的很有帮助,并且包含解释性 cmets 有很长的路要走。
    猜你喜欢
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 2014-04-14
    • 2011-11-16
    • 2015-05-22
    • 1970-01-01
    相关资源
    最近更新 更多