I have a matrix M defined as follows:
M <- matrix(1:6, nrow=2, byrow=TRUE)
1 2 3
4 5 6
and I want to generate all possible permutations for this matrix as a list. After reading Creating all the different permutations of the list in R , I tried using
library(combinat)
permn(M)
but this gives me all the permutations as a single row, and not the original 2 x 3 matrix.
So I get something like
[[1]]
[1] 1 4 2 5 3 6
[[2]]
[1] 1 4 2 5 6 3
[[3]]
[1] 1 4 2 6 5 3
...
[[720]]
[1] 4 1 2 5 3 6
But I want the first and second lines to be different from each other, so this will be a list that looks more like the following:
[[1]]
1 2 3
4 5 6
[[2]]
1 3 2
4 5 6
[[3]]
2 3 1
5 4 6
...
until I get all possible combinations of M. Is there a way to do this in R?
Thanks!
source
share