Learn R Programming

itertools (version 0.1-3)

isplitVector: Create an iterator that splits a vector

Description

Create an iterator that splits a vector into smaller pieces. You can specify either the number of pieces, using the chunks argument, or the maximum size of the pieces, using the chunkSize argument.

Usage

isplitVector(x, ...)

Arguments

x
Vector to iterate over. Note that it doesn't need to be an atomic vector, so a list is acceptable.
...
Passed as the second and subsequent arguments to idiv function. Currently, idiv accepts either a value for chunks or chunkSize.

Value

An iterator that returns vectors of the same type as x with one or more elements from x.

See Also

idiv

Examples

Run this code
# Split the vector 1:10 into "chunks" with a maximum length of three
it <- ihasNext(isplitVector(1:10, chunkSize=3))
while (hasNext(it)) {
  print(nextElem(it))
}

# Split the vector "letters" into four chunks
it <- ihasNext(isplitVector(letters, chunks=4))
while (hasNext(it)) {
  print(nextElem(it))
}

# Get the first five elements of a list as a list
nextElem(isplitVector(as.list(letters), chunkSize=5))

Run the code above in your browser using DataLab