我收到了有关如何在列表中创建一组列表的帮助,但是我无法添加另一层/扩展列表的深度。我想要的只是在每个列表中添加一个最后一个"层",上面写着"数据帧"、"数据帧2"等。目前我有:
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
,包含AA
,BB
,CC
和DD
,在每个列表中是一个进一步的列表,例如AA vs BB
、AA vs BB
等,或者在BB
的情况下,里面的列表将读取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
、DataFrame2
、Matrix
、Matrix2
。
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