| lexicographicPermutations {R.basic} | R Documentation |
Generate all lexicographic permutations of a vector. If the vector is of length N, there are N! permutation.
lexicographicPermutations(x)
x |
The vector from which all permutations should be generated. |
Returns a matrix with N columns and N! rows, where N is the length of the vector.
Henrik Bengtsson, http://www.braju.com/R/
See lexicographicPermutation() to generate (only) the n:th
lexicographic permutation of a vector.
See permutations() for a wrapper function to generate
different types of permutations.
See x[sample(n)] to generate a random permutation.
x <- c("a", "b", "c", "d")
p1 <- NULL
for (k in 1:factorial(length(x))-1)
p1 <- rbind(p1, lexicographicPermutation(x, k))
print(p1)
p2 <- lexicographicPermutations(x)
print(p2)
if (!identical(p1, p2))
stop("Permutations 'p1' and 'p2' should be identical!")