I would like to combine a list of list which has elements that are arrays and numbers, the array has attributes that I want to conserve and also the dimensions of the array.
# names of rows
pn <-as.list(paste0("P",1:10))
pa <-matrix(runif(100),nrow=10)
rownames(pa)<-pn
pb <-matrix(runif(100),nrow=10)
rownames(pb)<-pn
a<-list(list(nsim=runif(1),param=pa),list(nsim=runif(1),param=pb))
I can combine this list into one list like this
a1 <- do.call(Map, c(c, a))
but this is not the result I need, the names of the rows are lost and I want to make different operations for each component, for nsim I want to make a sum, and for param I want to combine by columns like cbind. I think that this could be done with a for loop but I wonder if it could be done faster using a function instead of Map. The result I expect is like this:
pc <-matrix(runif(200),nrow=10)
rownames(pc)<-pn
res<-list(nsim=runif(1)+runif(1),param=pa)
res
$nsim
[1] 0.3104111
$param
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
P1 0.2239347 0.06278946 0.98205058 0.61853458 0.02383296 0.002134358 0.8513100 0.5719191 0.5900750
P2 0.8692000 0.12358855 0.16389523 0.70464981 0.28209890 0.595703930 0.1770708 0.7762373 0.5342873
P3 0.3812888 0.78675818 0.58067408 0.75473889 0.26632764 0.665318861 0.4758030 0.4214253 0.1549026
...
[,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19]
P1 0.7276528 0.6331882 0.8154552 0.034903094 0.384146499 0.1222392 0.1048753 0.5124031 0.57319354
P2 0.9089065 0.4261310 0.4903192 0.680854370 0.331368439 0.0239536 0.4988695 0.2384391 0.28938142
P3 0.2615411 0.6219749 0.8196556 0.611008178 0.012309896 0.1656148 0.1684677 0.8212866 0.96902755
...