Supposing an example in R where we use the lapply function within a same function to create an one-level list:
lapply(1:5, function(x)
{
r <- sample(4,1)
i<-lapply(1:r, function (y)
{
matrix(rep(1*x*y,3))
})
i
})
The given result is a two-level list (list of lists) like this one:
[[1]]
[[1]][[1]]
[,1]
[1,] 1
[2,] 1
[3,] 1
[[2]]
[[2]][[1]]
[,1]
[1,] 2
[2,] 2
[3,] 2
[[2]][[2]]
[,1]
[1,] 4
[2,] 4
[3,] 4
[[2]][[3]]
[,1]
[1,] 6
[2,] 6
[3,] 6
[[2]][[4]]
[,1]
[1,] 8
[2,] 8
[3,] 8
[[3]]
[[3]][[1]]
[,1]
[1,] 3
[2,] 3
[3,] 3
[[3]][[2]]
[,1]
[1,] 6
[2,] 6
[3,] 6
# ...
Preserving both lapply functions (or similar ones) and matrices with equal dimensions, how we could get an one-level list thus?
[[1]]
[,1]
[1,] 1
[2,] 1
[3,] 1
[[2]]
[,1]
[1,] 2
[2,] 2
[3,] 2
[[3]]
[,1]
[1,] 4
[2,] 4
[3,] 4
# ...
lapplyalways returns alist. (That's what the l in the name stands for.) Usesapplyinstead to simplify.jis the object returned by your code, you may tryunlist(j,recursive=FALSE).