I want to add together all matrices in a list. Here is an example of what I'm trying to do:
## sets up the problem
m1 <- matrix(0,nrow=9,ncol=4)
row.pres <- lapply(1:4,function(x) seq(x,x+2))
m1.l <- lapply(1:4,function(y) {m1[row.pres[[y]],y] <- 1
return(m1)}
)
I want to add up all the elements of m1.l
to come up with a single matrix of the same dimension as each of them. Here is my solution:
test <- lapply(1:4,function(x) paste("m1.l[[",x,"]]",sep=''))
add.all <- paste(test,collapse="+")
eval(parse(text=add.all))
But there must be a better way! perhaps something via do.call
?