comboGroups | R Documentation |
Generate partitions of a vector into groups. See Create Combinations in R by Groups on https://stackoverflow.com for a direct use case of when the groups sizes are equal.
Produce results in parallel using the Parallel
or nThreads
arguments.
GMP support allows for exploration where the number of results is large.
The output is in lexicographical order by groups.
comboGroups(v, numGroups = NULL, grpSizes = NULL,
retType = "matrix", lower = NULL, upper = NULL,
Parallel = FALSE, nThreads = NULL)
v |
Source vector. If |
numGroups |
An Integer. The number of groups that the vector will be partitioned into. The default is |
grpSizes |
A vector of whole numbers representing the size of each group. The default is |
retType |
A string, "3Darray" or "matrix", that determines the shape of the output. The default is "matrix". Note, "3Darray" can only be used when the size of each group is uniform. When the size of each group varies, the return output will always be a matrix. |
lower |
The lower bound. Partitions of groups are generated lexicographically, thus utilizing this argument will determine which specific result to start generating from (e.g. |
upper |
The upper bound. Similar to |
Parallel |
Logical value indicating whether results should be generated in parallel using |
nThreads |
Specific number of threads to be used. The default is |
Conceptually, this problem can be viewed as generating all permutations of the vector v
and removing the within group permutations. To illustrate this, let us consider the case of generating partitions of 1:8
into 2 groups each of size 4.
To begin, generate the permutations of 1:8
and group the first/last four elements of each row.
Grp1 | Grp2 | |||||||
C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | |
R1 | | 1 | 2 | 3 | 4 | | | 5 | 6 | 7 | 8 | |
R2 | | 1 | 2 | 3 | 4 | | | 5 | 6 | 8 | 7 | |
R3 | | 1 | 2 | 3 | 4 | | | 5 | 7 | 6 | 8 | |
R4 | | 1 | 2 | 3 | 4 | | | 5 | 7 | 8 | 6 | |
R5 | | 1 | 2 | 3 | 4 | | | 5 | 8 | 6 | 7 | |
R6 | | 1 | 2 | 3 | 4 | | | 5 | 8 | 7 | 6 | |
Note that the permutations above are equivalent partitions of 2 groups of size 4 as only the last four elements are permuted. If we look at at the 25^{th}
lexicographical permutation, we observe our second distinct partition.
Grp1 | Grp2 | |||||||
C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | |
R24 | | 1 | 2 | 3 | 4 | | | 8 | 7 | 6 | 5 | |
R25 | | 1 | 2 | 3 | 5 | | | 4 | 6 | 7 | 8 | |
R26 | | 1 | 2 | 3 | 5 | | | 4 | 6 | 8 | 7 | |
R27 | | 1 | 2 | 3 | 5 | | | 4 | 7 | 6 | 8 | |
R28 | | 1 | 2 | 3 | 5 | | | 4 | 7 | 8 | 6 | |
Continuing on, we will reach the 3,457^{th}
lexicographical permutation, which represents the last result:
Grp1 | Grp2 | |||||||
C1 | C2 | C3 | C4 | C5 | C6 | C7 | C8 | |
R3454 | | 1 | 6 | 7 | 5 | | |8 | 3 | 4 | 2 | |
R3455 | | 1 | 6 | 7 | 5 | | |8 | 4 | 2 | 3 | |
R3456 | | 1 | 6 | 7 | 5 | | |8 | 4 | 3 | 2 | |
R3457 | | 1 | 6 | 7 | 8 | | | 2 | 3 | 4 | 5 | |
R3458 | | 1 | 6 | 7 | 8 | | |2 | 3 | 5 | 4 | |
For this small example, the method above will not be that computationally expensive. In fact, there are only 35 total partitions of 1:8
into 2 groups of size 4 out of a possible factorial(8) = 40320
permutations. However, just doubling the size of the vector will make this approach infeasible as there are over 10 trillion permutations of 1:16
.
The algorithm in comboGroups
avoids these duplicate partitions of groups by utilizing an efficient algorithm analogous to the std::next_permutation found in the standard algorithm library in C++.
By default, a matrix is returned with column names corresponding to the associated group. If retType = "3Darray"
, a named 3D array is returned.
The maximum number of partitions of groups that can be generated at one time is 2^{31} - 1
. Utilizing lower
and upper
makes it possible to generate additional combinations/permutations.
The length of grpSizes
must equal numGroups
if both grpSize
and numGroups
are provided.
Joseph Wood
## return a matrix
comboGroups(8, 2)
## or a 3 dimensional array
temp = comboGroups(8, 2, retType = "3Darray")
## view the first partition
temp[1, , ]
## Example with groups of varying size
comboGroups(8, grpSizes = c(3, 5))
total = comboGroupsCount(11, grpSizes = c(3, 3, 5))
## Start generating from particular index
comboGroups(11, grpSizes = c(3, 3, 5), lower = total - 20)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.