I have a list where each list element itself holds another list with several names objects. Each of these named object is a vector of the same length. My goal is to efficiently combine the related objects (those of the same name) into a matrix by concatening vectors.
Here’s an example of the type of structure I’m working with. However, in the current application it’s coming from mclapply as it is a parallelized multilevel model, and I don’t think there’s a way around getting a list of lists back.
> test=lapply(1:2,function(x){out = list(); out$t=rnorm(3)+x; out$p =rnorm(3)+ x+.1; return(out)})
> test
[[1]]
[[1]]$t
[1] 0.5950165 0.8827352 0.5614947
[[1]]$p
[1] 2.6144102 1.9688743 0.6241944
[[2]]
[[2]]$t
[1] 2.562030 1.832571 3.018756
[[2]]$p
[1] 1.7431969 0.5305784 2.6935106
Here’s a crude way to accomplish what I want
> t.matrix = cbind(test[[1]]$t,test[[2]]$t)
> t.matrix
[,1] [,2]
[1,] 2.2094525 2.634907
[2,] -0.2822453 2.440666
[3,] 1.1704518 2.483424
but instead I’d like to be able to do this for a very long list (around 1 million elements), and my current solution doesn’t scale.
I suppose I could use a for loop, but it seems like there must be a better way to do it with clever use of reduce or unlist or sapply or something like that.
Or, to process both sets of list elements at once: