• Returns an iterator for iterating over combinations or permutations of a vector.

  • Supports random access via the [[ method.

  • GMP support allows for exploration of combinations/permutations of vectors with many elements.

  • The output is in lexicographical order for the next methods and reverse lexicographical order for the prev methods.

comboIter(v, m = NULL, repetition = FALSE, freqs = NULL,
          constraintFun = NULL, comparisonFun = NULL,
          limitConstraints = NULL, FUN = NULL, tolerance = NULL)

permuteIter(v, m = NULL, repetition = FALSE, freqs = NULL,
            constraintFun = NULL, comparisonFun = NULL,
            limitConstraints = NULL, FUN = NULL, tolerance = NULL)

Arguments

v

Source vector. If v is an integer (including nonpositive integers), it will be converted to the sequence 1:v. All atomic types are supported (See ?is.atomic).

m

Number of elements to choose. If repetition = TRUE or freqs is utilized, m can exceed the length of v. If m = NULL, the length will default to length(v) or sum(freqs).

repetition

Logical value indicating whether combinations/permutations should be with or without repetition. The default is FALSE.

freqs

A vector of frequencies used for producing all combinations/permutations of a multiset of v. Each element of freqs represents how many times each element of the source vector, v, is repeated. It is analogous to the times argument in rep. The default value is NULL.

constraintFun

This feature will be available in future releases. The default is NULL.

comparisonFun

This feature will be available in future releases. The default is NULL.

limitConstraints

This feature will be available in future releases. The default is NULL.

FUN

Function to be applied to each combination/permutation. The default is NULL.

tolerance

This feature will be available in future releases. The default is NULL.

Value

A vector of length m is returned if nextIter or prevIter is called, otherwise a matrix is returned with each row containing a vector of length \(m\). If FUN is utilized and either nextIter or prevIter is called, the result will be determined by FUN, otherwise a list is returned.

Details

Once you initialize a new iterator, the following methods are available:

nextIter

Retrieve the next lexicographical result

nextNIter

Pass an integer n to retrieve the next n lexicographical results

nextRemaining

Retrieve all remaining lexicographical results

currIter

Returns the current iteration

prevIter

Retrieve the previous lexicographical result (the next reverse lexicographical result)

prevNIter

Pass an integer n to retrieve the previous n lexicographical results (the next n reverse lexicographical results)

prevRemaining

Retrieve all remaining reverse lexicographical results

startOver

Resets the iterator

sourceVector

View the source vector

summary

Returns a list of summary information about the iterator

front

Retrieve the first lexicographical result

back

Retrieve the last lexicographical result

[[

Random access method. Pass a single value or a vector of valid indices. If a single value is passed, the internal index of the iterator will be updated, however if a vector is passed the internal state will not change. GMP support allows for flexible indexing.

Note

  • The maximum number of combinations/permutations that can be generated at one time is \(2^{31} - 1\).

  • Factor vectors are accepted. Class and level attributes are preserved.

  • Currently, constrained iterators are not supported (Will be available in future releases).

See also

References

Examples

a = permuteIter(unique(state.region)) a$nextIter()
#> [1] South West Northeast North Central #> Levels: Northeast South North Central West
a$nextNIter(3)
#> [,1] [,2] [,3] [,4] #> [1,] South West North Central Northeast #> [2,] South Northeast West North Central #> [3,] South Northeast North Central West #> Levels: Northeast South North Central West
a$front()
#> [1] South West Northeast North Central #> Levels: Northeast South North Central West
a$nextRemaining()
#> [,1] [,2] [,3] [,4] #> [1,] South West North Central Northeast #> [2,] South Northeast West North Central #> [3,] South Northeast North Central West #> [4,] South North Central West Northeast #> [5,] South North Central Northeast West #> [6,] West South Northeast North Central #> [7,] West South North Central Northeast #> [8,] West Northeast South North Central #> [9,] West Northeast North Central South #> [10,] West North Central South Northeast #> [11,] West North Central Northeast South #> [12,] Northeast South West North Central #> [13,] Northeast South North Central West #> [14,] Northeast West South North Central #> [15,] Northeast West North Central South #> [16,] Northeast North Central South West #> [17,] Northeast North Central West South #> [18,] North Central South West Northeast #> [19,] North Central South Northeast West #> [20,] North Central West South Northeast #> [21,] North Central West Northeast South #> [22,] North Central Northeast South West #> [23,] North Central Northeast West South #> Levels: Northeast South North Central West
a$prevIter()
#> [1] North Central Northeast West South #> Levels: Northeast South North Central West
a$prevNIter(15)
#> [,1] [,2] [,3] [,4] #> [1,] North Central Northeast South West #> [2,] North Central West Northeast South #> [3,] North Central West South Northeast #> [4,] North Central South Northeast West #> [5,] North Central South West Northeast #> [6,] Northeast North Central West South #> [7,] Northeast North Central South West #> [8,] Northeast West North Central South #> [9,] Northeast West South North Central #> [10,] Northeast South North Central West #> [11,] Northeast South West North Central #> [12,] West North Central Northeast South #> [13,] West North Central South Northeast #> [14,] West Northeast North Central South #> [15,] West Northeast South North Central #> Levels: Northeast South North Central West
#> $description #> [1] "Permutations of 4 choose 4" #> #> $currentIndex #> [1] 9 #> #> $totalResults #> [1] 24 #> #> $totalRemaining #> [1] 15 #>
a$back()
#> [1] North Central Northeast West South #> Levels: Northeast South North Central West
a$prevRemaining()
#> [,1] [,2] [,3] [,4] #> [1,] North Central Northeast South West #> [2,] North Central West Northeast South #> [3,] North Central West South Northeast #> [4,] North Central South Northeast West #> [5,] North Central South West Northeast #> [6,] Northeast North Central West South #> [7,] Northeast North Central South West #> [8,] Northeast West North Central South #> [9,] Northeast West South North Central #> [10,] Northeast South North Central West #> [11,] Northeast South West North Central #> [12,] West North Central Northeast South #> [13,] West North Central South Northeast #> [14,] West Northeast North Central South #> [15,] West Northeast South North Central #> [16,] West South North Central Northeast #> [17,] West South Northeast North Central #> [18,] South North Central Northeast West #> [19,] South North Central West Northeast #> [20,] South Northeast North Central West #> [21,] South Northeast West North Central #> [22,] South West North Central Northeast #> [23,] South West Northeast North Central #> Levels: Northeast South North Central West
a[[5]]
#> [1] South North Central West Northeast #> Levels: Northeast South North Central West
#> $description #> [1] "Permutations of 4 choose 4" #> #> $currentIndex #> [1] 5 #> #> $totalResults #> [1] 24 #> #> $totalRemaining #> [1] 19 #>
a[[c(1, 17, 3)]]
#> [,1] [,2] [,3] [,4] #> [1,] South West Northeast North Central #> [2,] Northeast North Central South West #> [3,] South Northeast West North Central #> Levels: Northeast South North Central West
#> $description #> [1] "Permutations of 4 choose 4" #> #> $currentIndex #> [1] 5 #> #> $totalResults #> [1] 24 #> #> $totalRemaining #> [1] 19 #>