Return the set of permutations for a given set of values. The values can be numeric values, characters or factors. CombN computes the number of combinations with and without replacement and order, whereas CombSet returns the value sets.

Permn(x, sort = FALSE)

CombN(n, m, repl = FALSE, ord = FALSE)
CombSet(x, m, repl = FALSE, ord = FALSE, as.list = FALSE)

Arguments

x

a vector of numeric values or characters. Characters need not be unique.

n

number of elements from which to choose.

m

number of elements to choose. For CombSet can m be a numeric vector too.

repl

logical. Should repetition of the same element be allowed? Defaults to FALSE

ord

logical. Does the order matter? Default is FALSE.

sort

logical, defining if the result set should be sorted. Default is FALSE.

as.list

logical, defining if the results should be returned in a flat list, say every sample is a single element of the resulting list. Default is FALSE.

Details

The vector x need not contain unique values. The permutations will automatically be filtered for unique sets, if the same element is given twice or more.

Value

a matrix with all possible permutations or combinations of the values in x for Permn and CombSet

if m contains more than one element the result will be a list of matrices or a flat list if as.list is set to TRUE


an integer value for CombN

Author

Friederich Leisch <Friedrich.Leisch@boku.ac.at>
Andri Signorell <andri@signorell.net> (CombSet, CombN)

See also

combn, choose, factorial, CombPairs
vignette("Combinatorics")

Examples

Permn(letters[2:5])
#>       [,1] [,2] [,3] [,4]
#>  [1,] "b"  "c"  "d"  "e" 
#>  [2,] "c"  "b"  "d"  "e" 
#>  [3,] "c"  "d"  "b"  "e" 
#>  [4,] "b"  "d"  "c"  "e" 
#>  [5,] "d"  "b"  "c"  "e" 
#>  [6,] "d"  "c"  "b"  "e" 
#>  [7,] "c"  "d"  "e"  "b" 
#>  [8,] "b"  "d"  "e"  "c" 
#>  [9,] "d"  "b"  "e"  "c" 
#> [10,] "d"  "c"  "e"  "b" 
#> [11,] "b"  "c"  "e"  "d" 
#> [12,] "c"  "b"  "e"  "d" 
#> [13,] "d"  "e"  "b"  "c" 
#> [14,] "d"  "e"  "c"  "b" 
#> [15,] "b"  "e"  "c"  "d" 
#> [16,] "c"  "e"  "b"  "d" 
#> [17,] "c"  "e"  "d"  "b" 
#> [18,] "b"  "e"  "d"  "c" 
#> [19,] "e"  "b"  "c"  "d" 
#> [20,] "e"  "c"  "b"  "d" 
#> [21,] "e"  "c"  "d"  "b" 
#> [22,] "e"  "b"  "d"  "c" 
#> [23,] "e"  "d"  "b"  "c" 
#> [24,] "e"  "d"  "c"  "b" 
Permn(2:5)
#>       [,1] [,2] [,3] [,4]
#>  [1,]    2    3    4    5
#>  [2,]    3    2    4    5
#>  [3,]    3    4    2    5
#>  [4,]    2    4    3    5
#>  [5,]    4    2    3    5
#>  [6,]    4    3    2    5
#>  [7,]    3    4    5    2
#>  [8,]    2    4    5    3
#>  [9,]    4    2    5    3
#> [10,]    4    3    5    2
#> [11,]    2    3    5    4
#> [12,]    3    2    5    4
#> [13,]    4    5    2    3
#> [14,]    4    5    3    2
#> [15,]    2    5    3    4
#> [16,]    3    5    2    4
#> [17,]    3    5    4    2
#> [18,]    2    5    4    3
#> [19,]    5    2    3    4
#> [20,]    5    3    2    4
#> [21,]    5    3    4    2
#> [22,]    5    2    4    3
#> [23,]    5    4    2    3
#> [24,]    5    4    3    2

# containing the same element more than once
Permn(c("a", "b", "c", "a"))
#>       [,1] [,2] [,3] [,4]
#>  [1,] "a"  "b"  "c"  "a" 
#>  [2,] "b"  "a"  "c"  "a" 
#>  [3,] "b"  "c"  "a"  "a" 
#>  [4,] "a"  "c"  "b"  "a" 
#>  [5,] "c"  "a"  "b"  "a" 
#>  [6,] "c"  "b"  "a"  "a" 
#>  [7,] "a"  "c"  "a"  "b" 
#>  [8,] "c"  "a"  "a"  "b" 
#>  [9,] "a"  "b"  "a"  "c" 
#> [10,] "b"  "a"  "a"  "c" 
#> [11,] "a"  "a"  "b"  "c" 
#> [12,] "a"  "a"  "c"  "b" 


# only combinations of 2, but in every possible order
x <- letters[1:4]
n <- length(x)
m <- 2

# the samples
CombSet(x, m, repl=TRUE, ord=FALSE)
#>       [,1] [,2]
#>  [1,] "a"  "a" 
#>  [2,] "a"  "b" 
#>  [3,] "a"  "c" 
#>  [4,] "a"  "d" 
#>  [5,] "b"  "b" 
#>  [6,] "b"  "c" 
#>  [7,] "b"  "d" 
#>  [8,] "c"  "c" 
#>  [9,] "c"  "d" 
#> [10,] "d"  "d" 
CombSet(x, m, repl=TRUE, ord=TRUE)
#>       [,1] [,2]
#>  [1,] "a"  "a" 
#>  [2,] "b"  "a" 
#>  [3,] "c"  "a" 
#>  [4,] "d"  "a" 
#>  [5,] "a"  "b" 
#>  [6,] "b"  "b" 
#>  [7,] "c"  "b" 
#>  [8,] "d"  "b" 
#>  [9,] "a"  "c" 
#> [10,] "b"  "c" 
#> [11,] "c"  "c" 
#> [12,] "d"  "c" 
#> [13,] "a"  "d" 
#> [14,] "b"  "d" 
#> [15,] "c"  "d" 
#> [16,] "d"  "d" 
CombSet(x, m, repl=FALSE, ord=TRUE)
#>       [,1] [,2]
#>  [1,] "a"  "b" 
#>  [2,] "b"  "a" 
#>  [3,] "a"  "c" 
#>  [4,] "c"  "a" 
#>  [5,] "a"  "d" 
#>  [6,] "d"  "a" 
#>  [7,] "b"  "c" 
#>  [8,] "c"  "b" 
#>  [9,] "b"  "d" 
#> [10,] "d"  "b" 
#> [11,] "c"  "d" 
#> [12,] "d"  "c" 
CombSet(x, m, repl=FALSE, ord=FALSE)
#>      [,1] [,2]
#> [1,] "a"  "b" 
#> [2,] "a"  "c" 
#> [3,] "a"  "d" 
#> [4,] "b"  "c" 
#> [5,] "b"  "d" 
#> [6,] "c"  "d" 

# the number of the samples
CombN(n, m, repl=TRUE, ord=FALSE)
#> [1] 10
CombN(n, m, repl=TRUE, ord=TRUE)
#> [1] 16
CombN(n, m, repl=FALSE, ord=TRUE)
#> [1] 12
CombN(n, m, repl=FALSE, ord=FALSE)
#> [1] 6

# build all subsets of length 1, 3 and 5 and return a flat list
x <- letters[1:5]
CombSet(x=x, m=c(1, 3, 5), as.list=TRUE)
#> [[1]]
#> [1] "a"
#> 
#> [[2]]
#> [1] "b"
#> 
#> [[3]]
#> [1] "c"
#> 
#> [[4]]
#> [1] "d"
#> 
#> [[5]]
#> [1] "e"
#> 
#> [[6]]
#> [1] "a" "b" "c"
#> 
#> [[7]]
#> [1] "a" "b" "d"
#> 
#> [[8]]
#> [1] "a" "b" "e"
#> 
#> [[9]]
#> [1] "a" "c" "d"
#> 
#> [[10]]
#> [1] "a" "c" "e"
#> 
#> [[11]]
#> [1] "a" "d" "e"
#> 
#> [[12]]
#> [1] "b" "c" "d"
#> 
#> [[13]]
#> [1] "b" "c" "e"
#> 
#> [[14]]
#> [1] "b" "d" "e"
#> 
#> [[15]]
#> [1] "c" "d" "e"
#> 
#> [[16]]
#> [1] "a" "b" "c" "d" "e"
#>