I have two lists, each one with many matrices. For brevity, here is an example with two matrices per list:
l1 <- list(matrix(1:20, nrow=2),
matrix(21:40, nrow=2))
l2 <- list(matrix(41:60, nrow=2),
matrix(61:80, nrow=2))
I am trying to use lapply to (a) remove the first column of each matrix and then (b) multiply each matrix in l1 against the corresponding matrix in l2.
First, I tried:
result <- lapply(l1, function(x,y){
tmp <- x[,-1]*y[,-1]
return(tmp)
}, y=l2)
It didn't work. The problem is that I can't remove the first column from l2 this way. If l2 were a vector instead of a list, the code would work with tmp <- x[,-1]*y[-1]. But even when I try to simply multiply the matrices in each list, it still does not work:
result <- lapply(l1, function(x,y){
tmp <- x*y
return(tmp)
}, y=l2)
Any ideas?