permute: Generate all permutations of input

Description Usage Arguments Details Value Examples

View source: R/util.r

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

1

Arguments

x

list or vector

Details

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

Value

data frame, each row being one permutation

Examples

1
2
3
4
5
6
7
8
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)))

jackwasey/jwutil documentation built on Jan. 20, 2020, 6:56 p.m.