Appending a list to a list of lists in R
Solution 1
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
Solution 2
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:
outlist <- list(resultsa)
outlist <- append(outlist,list(resultsb))
outlist <- append(outlist,list(resultsc))
Same results.
Solution 3
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
.
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
Solution 4
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))))
}
Solution 5
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...
I used it for a complex implementation (with more than two levels) and it works!
SJWard
Updated on July 05, 2022Comments
-
SJWard almost 2 years
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:
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:
List to contain the other lists and be saved:
outlist1 <- list()
First iteration:
resultsa <- list(1,2,3,4,5)
outlist <- append(outlist1,resultsa)
Second Iteration:
resultsb <- list(6,7,8,9,10)
outlist <- append(outlist1,b)
Third Iteration:
resultsc <- list(11,12,13,14,15)
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.
-
SJWard about 11 yearsThat's exactly it. The second part enclosed in the list bracket's makes all the difference. Gives the format I need and prevents the Warning about multiples of object length. I had before when I tried the equivalent of
List[[length(List)+1]] <- sample(1:3)
to try and reach a solution. Thank you! -
sebastian-c about 11 yearsNitpick:
is.vector(list())
returns TRUE :) -
Assad Ebrahim about 9 yearsYou could also use c(.., ..) to concatenate the two "list of lists". You just have to make sure both arguments to c( ) are lists of lists.
-
chao about 9 yearsExcellent! The only useful example I found, although showing that R is a horrid language!
-
krevelen about 7 yearsWhile
newlist <- list(oldlist, list(someobj))
is the most popular answer given to this question, there are other methods given among other answers. The seemingly quirky list syntax is reminiscent of the elegant PROLOG lists, which using a head and tail part.