This function generates all the permutations of selecting k
items from n
items.
The results are in lexicographical order.
permutations(x = NULL, k = NULL, n = NULL, v = NULL, freq = NULL, replace = FALSE, layout = NULL, nitem = -1L, skip = NULL, index = NULL, nsample = NULL, drop = NULL)
x | an integer or a vector, will be treated as |
---|---|
k | an integer, the number of items drawn, defaults to |
n | an integer, the total number of items, its value may be implicitly deduced from |
v | a vector to be drawn, defaults to |
freq | an integer vector of item repeat frequencies |
replace | an logical to draw items with replacement |
layout | if "row", "column" or "list" is specified, the returned value would be a "row-major" matrix, a "column-major" matrix or a list respectively |
nitem | number of permutations required, usually used with |
skip | the number of permutations skipped |
index | a vector of indices of the desired permutations |
nsample | sampling random permutations |
drop | vectorize a matrix or unlist a list |
ipermutations for iterating permutations and npermutations to calculate number of permutations
permutations(3)#> [,1] [,2] [,3] #> [1,] 1 2 3 #> [2,] 1 3 2 #> [3,] 2 1 3 #> [4,] 2 3 1 #> [5,] 3 1 2 #> [6,] 3 2 1permutations(LETTERS[1:3])#> [,1] [,2] [,3] #> [1,] "A" "B" "C" #> [2,] "A" "C" "B" #> [3,] "B" "A" "C" #> [4,] "B" "C" "A" #> [5,] "C" "A" "B" #> [6,] "C" "B" "A"# choose 2 from 4 permutations(4, 2)#> [,1] [,2] #> [1,] 1 2 #> [2,] 1 3 #> [3,] 1 4 #> [4,] 2 1 #> [5,] 2 3 #> [6,] 2 4 #> [7,] 3 1 #> [8,] 3 2 #> [9,] 3 4 #> [10,] 4 1 #> [11,] 4 2 #> [12,] 4 3permutations(LETTERS[1:3], k = 2)#> [,1] [,2] #> [1,] "A" "B" #> [2,] "A" "C" #> [3,] "B" "A" #> [4,] "B" "C" #> [5,] "C" "A" #> [6,] "C" "B"#> [,1] [,2] [,3] #> [1,] 1 1 2 #> [2,] 1 2 1 #> [3,] 1 2 2 #> [4,] 2 1 1 #> [5,] 2 1 2 #> [6,] 2 2 1 #> [7,] 2 2 2# with replacement permutations(4, 2, replace = TRUE)#> [,1] [,2] #> [1,] 1 1 #> [2,] 1 2 #> [3,] 1 3 #> [4,] 1 4 #> [5,] 2 1 #> [6,] 2 2 #> [7,] 2 3 #> [8,] 2 4 #> [9,] 3 1 #> [10,] 3 2 #> [11,] 3 3 #> [12,] 3 4 #> [13,] 4 1 #> [14,] 4 2 #> [15,] 4 3 #> [16,] 4 4# column major permutations(3, layout = "column")#> [,1] [,2] [,3] [,4] [,5] [,6] #> [1,] 1 1 2 2 3 3 #> [2,] 2 3 1 3 1 2 #> [3,] 3 2 3 1 2 1permutations(4, 2, layout = "column")#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] #> [1,] 1 1 1 2 2 2 3 3 3 4 4 4 #> [2,] 2 3 4 1 3 4 1 2 4 1 2 3# list output permutations(3, layout = "list")#> [[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(4, 2, layout = "list")#> [[1]] #> [1] 1 2 #> #> [[2]] #> [1] 1 3 #> #> [[3]] #> [1] 1 4 #> #> [[4]] #> [1] 2 1 #> #> [[5]] #> [1] 2 3 #> #> [[6]] #> [1] 2 4 #> #> [[7]] #> [1] 3 1 #> #> [[8]] #> [1] 3 2 #> #> [[9]] #> [1] 3 4 #> #> [[10]] #> [1] 4 1 #> #> [[11]] #> [1] 4 2 #> #> [[12]] #> [1] 4 3 #># specifc range of permutations permutations(4, 2, nitem = 2, skip = 3)#> [,1] [,2] #> [1,] 2 1 #> [2,] 2 3#> [,1] [,2] #> [1,] 1 4 #> [2,] 2 3# random permutations permutations(4, 2, nsample = 3)#> [,1] [,2] #> [1,] 1 3 #> [2,] 1 2 #> [3,] 2 4#> [1] 1 0#> [1] 1 0#> [1] 0 6#> [1] 1 0#> [1] 0 1