Skip to contents

An analogue of the rep function operating on iterables.

Usage

i_rep(iterable, times = 1, length.out = NULL, each = 1, ...)

Arguments

iterable

The iterable to iterate over repeatedly.

times

How many times to recycle the underlying iteror (via i_recycle).

length.out

The maximum length of output. If this is given times is ignored.

each

The number of times to repeat each element. You can pass a vector (recycled), or another iterable, to repeat each element a varying number of times.

...

further arguments passed along to iteror(iterable, ...)

Value

an iteror yilding and repeating values from iterable.

Details

Note that arguments times and each can work slightly differently from rep; times must always be of length 1; to repeat each element a specific number of times, provide a vector to each rather than times.

Originally from the itertools package.

See also

Examples


as.numeric(i_rep(1:4, 2))
#> [1] 1 2 3 4 1 2 3 4
as.numeric(i_rep(1:4, each=2))
#> [1] 1 1 2 2 3 3 4 4
as.numeric(i_rep(1:4, each=c(2,2,2,2)))
#> [1] 1 1 2 2 3 3 4 4
as.numeric(i_rep(1:4, each=c(2,1,2,1)))
#> [1] 1 1 2 3 3 4
as.numeric(i_rep(1:4, each=2, len=4))
#> [1] 1 1 2 2
as.numeric(i_rep(1:4, each=2, len=10))
#>  [1] 1 1 2 2 3 3 4 4 1 1
as.numeric(i_rep(1:4, each=2, times=3))
#>  [1] 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4

# Note `rep` makes `times` behave like `each` when given a vector.
# `i_rep` does not reproduce this behavior; give the vector to `each`.
# These are equivalent:
as.numeric(i_rep(1:4, each = 1:8, times=2))
#>  [1] 1 2 2 3 3 3 4 4 4 4 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4
rep(rep(1:4, times=2), times=1:8)
#>  [1] 1 2 2 3 3 3 4 4 4 4 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4