Skip to contents

This function creates all permutations of a given vector.

Usage

permutations(x)

Arguments

x

[atomic()]
Any vector.

Value

A list of all permutations of x.

References

Modified version of https://stackoverflow.com/a/20199902/15157768.

Examples

permutations(1:3)
#> [[1]]
#> [1] 1 2 3
#> 
#> [[2]]
#> [1] 1 3 2
#> 
#> [[3]]
#> [1] 2 1 3
#> 
#> [[4]]
#> [1] 2 3 1
#> 
#> [[5]]
#> [1] 3 1 2
#> 
#> [[6]]
#> [1] 3 2 1
#> 
permutations(LETTERS[1:3])
#> [[1]]
#> [1] "A" "B" "C"
#> 
#> [[2]]
#> [1] "A" "C" "B"
#> 
#> [[3]]
#> [1] "B" "A" "C"
#> 
#> [[4]]
#> [1] "B" "C" "A"
#> 
#> [[5]]
#> [1] "C" "A" "B"
#> 
#> [[6]]
#> [1] "C" "B" "A"
#>