list 将列表附加到 R 中的列表列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14848172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:57:23  来源:igfitidea点击:

Appending a list to a list of lists in R

rlistmatrixappendextend

提问by Ward9250

I'm having issues appending data to a list which is already in a list format. I have a program which will export results objects during a simulation loop. The data itself is stored as a list of matrices. My idea is to store those lists in a list, and then save this list of lists as an R object for later analysis, however I'm having some issues achieving this correctly. I'll show what I've done with small abstract example just using values instead of the matrix data from my simulation:

我在将数据附加到已经是列表格式的列表时遇到问题。我有一个程序可以在模拟循环期间导出结果对象。数据本身存储为矩阵列表。我的想法是将这些列表存储在一个列表中,然后将此列表列表保存为 R 对象以供以后分析,但是我在正确实现这一点时遇到了一些问题。我将展示我使用小抽象示例所做的工作,仅使用值而不是模拟中的矩阵数据:

Say I've run the simulation loop for 3 times. During the iterations, the results lists need to be collected into the one list of lists that I will save as an R object:

假设我已经运行了 3 次模拟循环。在迭代过程中,需要将结果列表收集到我将保存为 R 对象的一个​​列表列表中:

List to contain the other lists and be saved: outlist1 <- list()

包含其他列表并保存的列表: outlist1 <- list()

First iteration: resultsa <- list(1,2,3,4,5)

第一次迭代: resultsa <- list(1,2,3,4,5)

outlist <- append(outlist1,resultsa)

outlist <- append(outlist1,resultsa)

Second Iteration: resultsb <- list(6,7,8,9,10)

第二次迭代: resultsb <- list(6,7,8,9,10)

outlist <- append(outlist1,b)

outlist <- append(outlist1,b)

Third Iteration: resultsc <- list(11,12,13,14,15)

第三次迭代: resultsc <- list(11,12,13,14,15)

outlist <- list(outlist2,c)

outlist <- list(outlist2,c)

However, this solution does not work with growing a list containing lists this way, the desired result is:

但是,此解决方案不适用于以这种方式增长包含列表的列表,所需的结果是:

>outlist
[[1]]
[[1]][[1]]
[1] 1

[[1]][[2]]
[1] 2

[[1]][[3]]
[1] 3

[[1]][[4]]
[1] 4

[[1]][[5]]
[1] 5


[[2]]
[[2]][[1]]
[1] 6

[[2]][[2]]
[1] 7

[[2]][[3]]
[1] 8

[[2]][[4]]
[1] 9

[[2]][[5]]
[1] 10


[[3]]
[[3]][[1]]
[1] 11

[[3]][[2]]
[1] 12

[[3]][[3]]
[1] 13

[[3]][[4]]
[1] 14

[[3]][[5]]
[1] 15

However, instead what I get is:

然而,我得到的是:

> outlist3
[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[1] 1

[[1]][[1]][[2]]
[1] 2

[[1]][[1]][[3]]
[1] 3

[[1]][[1]][[4]]
[1] 4

[[1]][[1]][[5]]
[1] 5


[[1]][[2]]
[[1]][[2]][[1]]
[1] 6

[[1]][[2]][[2]]
[1] 7

[[1]][[2]][[3]]
[1] 8

[[1]][[2]][[4]]
[1] 9

[[1]][[2]][[5]]
[1] 10

How do I grow a list, such that the resulting list formatted is like the desired result? If I do further analysis on these list I need to be able to easily access the elements.

如何增加列表,使格式化的结果列表与所需的结果类似?如果我对这些列表进行进一步分析,我需要能够轻松访问这些元素。

回答by Daniel Fischer

Could it be this, what you want to have:

难道是这样,你想要什么:

# Initial list:
myList <- list()

# Now the new experiments
for(i in 1:3){
  myList[[length(myList)+1]] <- list(sample(1:3))
}

myList

回答by IRTFM

outlist <- list(resultsa)
outlist[2] <- list(resultsb)
outlist[3] <- list(resultsc)

append's help file says it is for vectors. But it can be used here. I thought I had tried that before but there were some strange anomalies in the OP's code that may have mislead me:

append的帮助文件说它是用于向量的。但是这里可以使用。我以为我以前尝试过,但是 OP 的代码中有一些奇怪的异常可能误导了我:

outlist <- list(resultsa)
outlist <- append(outlist,list(resultsb))
outlist <- append(outlist,list(resultsc))

Same results.

结果一样。

回答by Brian Diggs

There are two other solutions which involve assigning to an index one past the end of the list. Here is a solution that does use append.

还有另外两种解决方案涉及将索引分配给列表末尾的索引。这是一个确实使用append.

resultsa <- list(1,2,3,4,5)
resultsb <- list(6,7,8,9,10)
resultsc <- list(11,12,13,14,15)

outlist <- list(resultsa)
outlist <- append(outlist, list(resultsb))
outlist <- append(outlist, list(resultsc))

which gives your requested format

这给出了您要求的格式

> str(outlist)
List of 3
 $ :List of 5
  ..$ : num 1
  ..$ : num 2
  ..$ : num 3
  ..$ : num 4
  ..$ : num 5
 $ :List of 5
  ..$ : num 6
  ..$ : num 7
  ..$ : num 8
  ..$ : num 9
  ..$ : num 10
 $ :List of 5
  ..$ : num 11
  ..$ : num 12
  ..$ : num 13
  ..$ : num 14
  ..$ : num 15

回答by Eduardo Bergel

This answer is similar to the accepted one, but a bit less convoluted.

这个答案与公认的相似,但不那么复杂。

L<-list()
for (i in 1:3) {
L<-c(L, list(list(sample(1:3))))
}

回答by Jesse Burstr?m

By putting an assignment of list on a variable first

首先将列表的赋值放在变量上

myVar <- list()

it opens the possibility of hiearchial assignments by

它开启了等级分配的可能性

myVar[[1]] <- list()
myVar[[2]] <- list()

and so on... so now it's possible to do

等等......所以现在可以做

myVar[[1]][[1]] <- c(...)
myVar[[1]][[2]] <- c(...)

or

或者

myVar[[1]][['subVar']] <- c(...)

and so on

等等

it is also possible to assign directly names (instead of $)

也可以直接指定名称(而不是 $)

myVar[['nameofsubvar]] <- list()

and then

进而

myVar[['nameofsubvar]][['nameofsubsubvar']] <- c('...')

important to remember is to always use double brackets to make the system work

重要的是要记住始终使用双括号使系统工作

then to get information is simple

那么获取信息很简单

myVar$nameofsubvar$nameofsubsubvar

and so on...

等等...

example:

例子:

a <-list()
a[['test']] <-list()
a[['test']][['subtest']] <- c(1,2,3)
a
$test
$test$subtest
[1] 1 2 3


a[['test']][['sub2test']] <- c(3,4,5)
a
$test
$test$subtest
[1] 1 2 3

$test$sub2test
[1] 3 4 5

a nice feature of the R language in it's hiearchial definition...

R 语言的一个很好的特性是它的层次结构定义......

I used it for a complex implementation (with more than two levels) and it works!

我将它用于复杂的实现(具有两个以上的级别)并且它有效!

回答by Paul

Just a note on Brian's answer below, the first assignment to outlistcan also be an appendstatement so you could also do something like this:

只是对下面布赖恩的回答做一个说明,第一个赋值outlist也可以是一个append陈述,所以你也可以做这样的事情:

resultsa <- list(1,2,3,4,5)
resultsb <- list(6,7,8,9,10)
resultsc <- list(11,12,13,14,15)

outlist <- list()
outlist <- append(outlist,list(resultsa))
outlist <- append(outlist, list(resultsb))
outlist <- append(outlist, list(resultsc))

This is sometimes helpful if you want to build a list from scratch in a loop.

如果您想在循环中从头开始构建列表,这有时会很有帮助。