将列表添加到R中的列表列表中。

时间:2021-07-31 18:15:41

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 < -列表(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.

我如何生成一个列表,这样生成的列表就像期望的结果一样?如果我对这些列表做进一步的分析,我需要能够很容易地访问这些元素。

5 个解决方案

#1


55  

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

#2


13  

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.

同样的结果。

#3


9  

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

#4


7  

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))))
}

#5


4  

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语言的一个很好的特点是hiearchial的定义…

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

我使用它来实现一个复杂的实现(超过两个级别),而且它可以工作!

#1


55  

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

#2


13  

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.

同样的结果。

#3


9  

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

#4


7  

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))))
}

#5


4  

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语言的一个很好的特点是hiearchial的定义…

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

我使用它来实现一个复杂的实现(超过两个级别),而且它可以工作!