Learn R Programming

jwutil (version 1.2.3)

permute: Generate all permutations of input

Description

Systematically permute the input vector or list, which is very slow for long x. Am amazed something this simple isn't either in base R, or in a straightforward form in a package.

Usage

permute(x)

Arguments

x

list or vector

Value

data frame, each row being one permutation

Details

TODO: limit to a certain cut-off, after which we randomly sample

Examples

Run this code
# NOT RUN {
ltr <- c("a", "b", "c", "d")
x <- permute(ltr)
print(x)
stopifnot(nrow(x) == factorial(length(ltr)))
ltr <- c("a", "b", "b")
x <- permute(ltr)
print(x)
stopifnot(nrow(x) == factorial(length(ltr)))
# }

Run the code above in your browser using DataLab