lexicographicPermutations {R.basic}R Documentation

Generate all lexicographic permutations of a vector

Description

Generate all lexicographic permutations of a vector. If the vector is of length N, there are N! permutation.

Usage

  lexicographicPermutations(x)

Arguments

x The vector from which all permutations should be generated.

Value

Returns a matrix with N columns and N! rows, where N is the length of the vector.

Author(s)

Henrik Bengtsson, http://www.braju.com/R/

References

  • A.G. Thakurta, Lexicographic Permutation Analysis, http://www.cs.wpi.edu/~dobrush/cs504/f02/projects/Anupama.htm

    See Also

    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.

    Examples

    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!")
    
    

    [Package Contents]