ichunk {itertools2} | R Documentation |
Iterator that returns elements in fixed-length chunks
Description
Constructs an iterator that returns elements of an iterable object
in
fixed-length chunks. If the length of the iterator is not divisible by
chunk_size
, the remainder of the last block is filled with the value
specified in fill
.
Usage
ichunk(object, chunk_size = 1, fill = NA)
Arguments
object |
an iterable object |
chunk_size |
the number of elements returned per chunk |
fill |
the value with which to fill the last chunk if the length of the
iterator is not divisble by |
Details
This function corresponds to Python's grouper
function. We chose the
name ichunk
because it more explicitly defines the function's purpose.
Value
each call to nextElem
results in a list of length
chunk_size
Examples
it <- ichunk(iterators::iter(1:5), chunk_size=2)
# List: list(1, 2, 3)
iterators::nextElem(it)
# List: list(4, 5, NA)
iterators::nextElem(it)
it2 <- ichunk(levels(iris$Species), chunk_size=4, "weeee")
# Returns: list("setosa", "versicolor", "virginica", "weeee")
iterators::nextElem(it2)
[Package itertools2 version 0.1.1 Index]