Learn R Programming

arrangements (version 1.1.7)

Permutations: Permutations iterator

Description

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.

Usage

Permutations

ipermutations(x, k = n, 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

k

an integer, the number of items drawn

n

an integer, the total number of items, its actual value may be determined by other variables

v

a vector to be drawn

freq

an integer vector of item repeat frequencies

replace

an logical to draw items with replacement

skip

the number of combinations skipped

Format

An object of class R6ClassGenerator of length 24.

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

Examples

Run this code
# NOT RUN {
iperm <- ipermutations(5, 2)
iperm$getnext()
iperm$getnext(2)
iperm$getnext(layout = "column", drop = FALSE)
# collect remaining permutations
iperm$collect()

library(foreach)
foreach(x = ipermutations(5, 2), .combine=c) %do% {
  sum(x)
}
# }

Run the code above in your browser using DataLab