Skip to content
Prev 206910 / 398506 Next

permutations from vectors out of a matrix

Sorry, wrong button. Below a hopefully more helpful solution...

Etienne,
I don't see the point in avoiding some 'special' packages. If you are
willing to change your mind in this regard, try one of the following
solutions that work for me:

library(combinat)
apply(mat, 2, function(x) unique(permn(x)))

# each object in the list contains the permutations from once column of
mat:
apply(mat, 2, function(x) do.call(rbind, unique(permn(x))))		

# all vectors you wanted in one matrix; note that they are in the rows,
so you might want to transpose this:
do.call(rbind, apply(mat, 2, function(x) do.call(rbind,
unique(permn(x)))))	

Not sure about the size of your original problem, though, it might take
a while. If you still want to avoid the (small!) package, you might
consider copying the code for permn from combinat to define the function
within your file. I guess it works (but didn't check) as it does not
seem to require any of the other functions of the package.

HTH, Michael