I'm a beginner i n R. I try to combine a series of matrices with a series of dimnames to obtain a set of permutations. I choosed to build something by expand.grid with each subset containing one matrix and one dimnames.
M1=matrix(c(0,1,0,1,0,0,0,0,1), nrow=3,ncol=3,byrow=T)
M2=matrix(c(0,0,1,0,1,0,1,0,0), nrow=3,ncol=3,byrow=T)
Mlist<-list(M1,M2)
Na<-list(c("A","B","C"),c("A","B","C"))
Nb<-list(c("B","A","C"),c("B","A","C"))
Nlist<-list(Na,Nb)
M<-expand.grid(Nlist,Mlist)
This works fine, but now, how to assemble each matrix with each dimnames with a function and lapply that will do the following for each row :
Mat1<-matrix(M[[2]][[1]],dimnames=list(M[[1]][[1]][[1]],M[[1]][[1]][[2]]),nrow=3,ncol=3,byrow=T)
Mat2<-matrix(M[[2]][[2]],dimnames=list(M[[1]][[2]][[1]],M[[1]][[2]][[1]]),nrow=3,ncol=3,byrow=T)
At the end, I should get something like this :
M1
A B C
A 0 1 0
B 1 0 0
C 0 0 1
M2
B A C
B 0 1 0
A 1 0 0
C 0 0 1
M3
A B C
A 0 0 1
B 0 1 0
C 1 0 0
M4
B A C
B 0 0 1
A 0 1 0
C 1 0 0
What I'm unable to find is how to set a variable that will be incremented at each step, something like x here :
Mat<-matrix(M[[2]][[x]],dimnames=list(M[[1]][[x]][[1]],M[[1]][[x]][[2]]),nrow=3,ncol=3,byrow=T)
Thank you