I have 3 datasets I permuted 10x. Each permutation of a dataset makes a column in 3 matrices (one matrix per dataset). These 3 matrices (m1
,m2
,m3
) are in a list L
. I would like to interrogate all possible combinations (10x10x10=1000) for each entry (in this case 4). I have used expand.grid to supply all combinations of column calls across the 3 matrices in the row of another matrix M
:
M<-expand.grid(seq(1:10),seq(1:10),seq(1:10))
Here is my data in a list:
m1<-matrix(c(1,2,1,0,3,2,1,2,3,4),nrow=4, ncol=10)
m2<-matrix(c(m1[1,]),nrow=4,ncol=10)
m3<-matrix(c(m1[2,]),nrow=4,ncol=10)
L<-list(m1, m2, m3)
Can you help me use do.call
, cbind
, lapply
/sapply
to efficiently retrieve the column coordinates from M to interrogate the corresponding columns in the 3 matrices contained in L and bind them to a new matrix as such:
m.res<-for (i in 1:nrow(M) { "get" L[[1:3]][M[i,]] }
For i=1
, m.res
would yield:
1 1 2
2 3 2
1 3 4
0 1 0
I clearly need a tutorial for lapply
/sapply
as this should not be this difficult.