Learn R Programming

iterors (version 1.0)

i_window: Construct a sliding window over an iterator

Description

Each element returned by i_window(obj) consists of n consecutive elements from the underlying obj, with the window advancing forward by one element each iteration.

Usage

i_window(obj, n, tail, ...)

Value

an iteror.

Arguments

obj

An iterable.

n

The width of the window to apply

tail

If a value is given, tails will be included at the beginning and end of iteration, filled with the given value.

...

passed along to iteror(object, ...)

Author

Peter Meilstrup

Examples

Run this code

#' @examples
it <- i_window(iteror(letters[1:4]), 2)
nextOr(it, NA) # list("a", "b")
nextOr(it, NA) # list("b", "c")
nextOr(it, NA) # list("c", "d")

it2 <- i_window(icount(5), 2)
nextOr(it2, NA) # list(1, 2)
nextOr(it2, NA) # list(2, 3)
nextOr(it2, NA) # list(3, 4)
nextOr(it2, NA) # list(4, 5)

it <- i_window(letters[1:4], 2)
nextOr(it, NA) # list("a", "b")
nextOr(it, NA) # list("b", "c")
nextOr(it, NA) # list("c", "d")

it <- i_window(letters[1:4], 3)
nextOr(it) # list("a", "b", "c")
nextOr(it) # list("b", "c", "d")

it <- i_window(letters[1:4], 3, tail=" ")
nextOr(it) # list(" ", " ", "a")
nextOr(it) # list(" ", "a", "b")
nextOr(it) # list("a", "b", "c")
nextOr(it) # list("b", "c", "d")
nextOr(it) # list("c", "d", " ")
nextOr(it) # list("d", " ", " ")

Run the code above in your browser using DataLab