This function partitions an non-negative interger n
into k
parts or parts of any sizes.
The results are in lexicographical or reversed lexicographical order.
partitions(n, k = NULL, distinct = FALSE, descending = FALSE, layout = NULL, nitem = -1L, skip = NULL, index = NULL, nsample = NULL, drop = NULL)
n | an non-negative integer to be partitioned |
---|---|
k | number of parts |
distinct | an logical to restrict distinct values |
descending | an logical to use reversed lexicographical order |
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 partitions required, usually used with |
skip | the number of partitions skipped |
index | a vector of indices of the desired partitions |
nsample | sampling random partitions |
drop | vectorize a matrix or unlist a list |
ipartitions for iterating partitions and npartitions to calculate number of partitions
# all partitions of 6 partitions(6)#> [,1] [,2] [,3] [,4] [,5] [,6] #> [1,] 1 1 1 1 1 1 #> [2,] 1 1 1 1 2 0 #> [3,] 1 1 1 3 0 0 #> [4,] 1 1 2 2 0 0 #> [5,] 1 1 4 0 0 0 #> [6,] 1 2 3 0 0 0 #> [7,] 1 5 0 0 0 0 #> [8,] 2 2 2 0 0 0 #> [9,] 2 4 0 0 0 0 #> [10,] 3 3 0 0 0 0 #> [11,] 6 0 0 0 0 0# reversed lexicographical order partitions(6, descending = TRUE)#> [,1] [,2] [,3] [,4] [,5] [,6] #> [1,] 6 0 0 0 0 0 #> [2,] 5 1 0 0 0 0 #> [3,] 4 2 0 0 0 0 #> [4,] 4 1 1 0 0 0 #> [5,] 3 3 0 0 0 0 #> [6,] 3 2 1 0 0 0 #> [7,] 3 1 1 1 0 0 #> [8,] 2 2 2 0 0 0 #> [9,] 2 2 1 1 0 0 #> [10,] 2 1 1 1 1 0 #> [11,] 1 1 1 1 1 1# fixed number of parts partitions(10, 5)#> [,1] [,2] [,3] [,4] [,5] #> [1,] 1 1 1 1 6 #> [2,] 1 1 1 2 5 #> [3,] 1 1 1 3 4 #> [4,] 1 1 2 2 4 #> [5,] 1 1 2 3 3 #> [6,] 1 2 2 2 3 #> [7,] 2 2 2 2 2# reversed lexicographical order partitions(10, 5, descending = TRUE)#> [,1] [,2] [,3] [,4] [,5] #> [1,] 6 1 1 1 1 #> [2,] 5 2 1 1 1 #> [3,] 4 3 1 1 1 #> [4,] 4 2 2 1 1 #> [5,] 3 3 2 1 1 #> [6,] 3 2 2 2 1 #> [7,] 2 2 2 2 2# column major partitions(6, layout = "column")#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] #> [1,] 1 1 1 1 1 1 1 2 2 3 6 #> [2,] 1 1 1 1 1 2 5 2 4 3 0 #> [3,] 1 1 1 2 4 3 0 2 0 0 0 #> [4,] 1 1 3 2 0 0 0 0 0 0 0 #> [5,] 1 2 0 0 0 0 0 0 0 0 0 #> [6,] 1 0 0 0 0 0 0 0 0 0 0partitions(6, 3, layout = "column")#> [,1] [,2] [,3] #> [1,] 1 1 2 #> [2,] 1 2 2 #> [3,] 4 3 2# list output partitions(6, layout = "list")#> [[1]] #> [1] 1 1 1 1 1 1 #> #> [[2]] #> [1] 1 1 1 1 2 #> #> [[3]] #> [1] 1 1 1 3 #> #> [[4]] #> [1] 1 1 2 2 #> #> [[5]] #> [1] 1 1 4 #> #> [[6]] #> [1] 1 2 3 #> #> [[7]] #> [1] 1 5 #> #> [[8]] #> [1] 2 2 2 #> #> [[9]] #> [1] 2 4 #> #> [[10]] #> [1] 3 3 #> #> [[11]] #> [1] 6 #>partitions(6, 3, layout = "list")#> [[1]] #> [1] 1 1 4 #> #> [[2]] #> [1] 1 2 3 #> #> [[3]] #> [1] 2 2 2 #>#> [1] 1 0#> [1] 0 0#> [1] 0 6#> [1] 1 0#> [1] 0 1