我已经收到了关于如何在列表中创建一组列表的帮助,但是我无法添加另一层/扩展列表的深度。我只想在每个列表中添加一个最后的“层”,即“DataFrame”、“DataFrame 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。
发布于 2019-04-12 07:40:05
lapply循环遍历一个类似结构的列表中的每个元素,并对其应用一个函数。值得注意的是,它不包括位置参数。
你想做的是:
Layer1的所有元素并为每个元素创建一个列表,该列表依次Layer1这些元素的wach包含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)4美元AA :4 ..$ AA与AA的列表:4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 : NULL ..$ AA对BB:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 : NULL ..$ AA与CC:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 : NULL ..$ AA与DD:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 : NULL $ BB :4 ..$ BB列表与AA:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 : NULL ..$ BB与BB:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 :空..$ BB对CC:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 : NULL ..$ BB与DD:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 :空$ CC :4 ..$ CC列表与AA:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 :空..$ CC与BB:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 :空..$ CC与CC:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 :空..$ CC与DD:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 : NULL $ DD :4 ..$ DD列表与AA:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 : NULL ..$ DD与BB:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 :空..$ DD与CC:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 : NULL ..$ DD与DD:列表4...$ DataFrame :空。..$ DataFrame2:空。..$矩阵:空。..$ Matrix2 : NULL
https://stackoverflow.com/questions/55646455
复制相似问题