This function returns a Permutations iterator for iterating permutations of k items from n items. The iterator allows users to fetch the next permutation(s) via the getnext() method.

ipermutations(
  x = NULL,
  k = NULL,
  n = NULL,
  v = NULL,
  freq = NULL,
  replace = FALSE,
  skip = NULL
)

Arguments

x

an integer or a vector, will be treated as n if integer; otherwise, will be treated as v. Should not be specified together with n and v.

k

an integer, the number of items drawn, defaults to n if freq is NULL else sum(freq)

n

an integer, the total number of items, its value may be implicitly deduced from length(v) or length(freq)

v

a vector to be drawn, defaults to 1:n.

freq

an integer vector of item repeat frequencies

replace

an logical to draw items with replacement

skip

the number of combinations skipped

Details

The Permutations class can be initialized by using the convenient wrapper ipermutations or


Permutations$new(n, k, v = NULL, freq = NULL, replace = FALSE)

getnext(d = 1L, layout = NULL, drop = NULL)
collect(layout = "row")
reset()
d

number of fetched arrangements

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

drop

vectorize a matrix or unlist a list

See also

permutations for generating all permutations and npermutations to calculate number of permutations

Super classes

arrangements::abstractiter -> arrangements::iter -> arrangements::Arrangements -> Permutations

Methods

Inherited methods


Method new()

Usage

Permutations$new(
  n,
  k = NULL,
  v = NULL,
  freq = NULL,
  replace = FALSE,
  skip = NULL
)


Method reset()

Usage

Permutations$reset()


Method collect()

Usage

Permutations$collect(layout = "row")


Method getnext()

Usage

Permutations$getnext(d = 1L, layout = NULL, drop = NULL)


Method print()

Usage

Permutations$print(...)


Method clone()

The objects of this class are cloneable with this method.

Usage

Permutations$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

iperm <- ipermutations(5, 2)
iperm$getnext()
#> [1] 1 2
iperm$getnext(2)
#>      [,1] [,2]
#> [1,]    1    3
#> [2,]    1    4
iperm$getnext(layout = "column", drop = FALSE)
#>      [,1]
#> [1,]    1
#> [2,]    5
# collect remaining permutations
iperm$collect()
#>       [,1] [,2]
#>  [1,]    2    1
#>  [2,]    2    3
#>  [3,]    2    4
#>  [4,]    2    5
#>  [5,]    3    1
#>  [6,]    3    2
#>  [7,]    3    4
#>  [8,]    3    5
#>  [9,]    4    1
#> [10,]    4    2
#> [11,]    4    3
#> [12,]    4    5
#> [13,]    5    1
#> [14,]    5    2
#> [15,]    5    3
#> [16,]    5    4

library(foreach)
#> Error in library(foreach): there is no package called ‘foreach’
foreach(x = ipermutations(5, 2), .combine=c) %do% {
  sum(x)
}
#> Error in foreach(x = ipermutations(5, 2), .combine = c) %do% {    sum(x)}: could not find function "%do%"