Skip to contents

The resulting iterator aggregates one element from each of the iterables into a list for each iteration. Used for lock-step iteration over several iterables at a time.

Usage

i_zip(...)

i_zip_longest(..., fill = NA)

Arguments

...

multiple arguments to iterate through in parallel

fill

the value used to replace missing values when the iterables in ... are of uneven length

Value

iterator that iterates through each argument in sequence

Details

For [i_zip], the output will finish when any of the underlying iterables finish.

Originally from the itertools package.

Originally from package itertools2.

Examples


# Iterate over two iterables of different sizes
as.list(i_zip(a=1:2, b=letters[1:3]))
#> [[1]]
#> [[1]]$a
#> [1] 1
#> 
#> [[1]]$b
#> [1] "a"
#> 
#> 
#> [[2]]
#> [[2]]$a
#> [1] 2
#> 
#> [[2]]$b
#> [1] "b"
#> 
#> 

it <- i_zip_longest(x=1:3, y=4:6, z=7:9)
nextOr(it, NA) # list(x=1, y=4, z=7)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 4
#> 
#> [[3]]
#> [1] 7
#> 
nextOr(it, NA) # list(x=2, y=5, z=8)
#> [[1]]
#> [1] 2
#> 
#> [[2]]
#> [1] 5
#> 
#> [[3]]
#> [1] 8
#> 
nextOr(it, NA) # list(x=3, y=6, z=9)
#> [[1]]
#> [1] 3
#> 
#> [[2]]
#> [1] 6
#> 
#> [[3]]
#> [1] 9
#> 

it2 <- i_zip_longest(1:3, 4:8)
nextOr(it2, NA) # list(1, 4)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 4
#> 
nextOr(it2, NA) # list(2, 5)
#> [[1]]
#> [1] 2
#> 
#> [[2]]
#> [1] 5
#> 
nextOr(it2, NA) # list(3, 6)
#> [[1]]
#> [1] 3
#> 
#> [[2]]
#> [1] 6
#> 
nextOr(it2, NA) # list(NA, 7)
#> [[1]]
#> [1] NA
#> 
#> [[2]]
#> [1] 7
#> 
nextOr(it2, NA) # list(NA, 8)
#> [[1]]
#> [1] NA
#> 
#> [[2]]
#> [1] 8
#> 

it3 <- i_zip_longest(1:2, 4:7, levels(iris$Species), fill="w00t")
nextOr(it3, NA) # list(1, 4, "setosa")
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 4
#> 
#> [[3]]
#> [1] "setosa"
#> 
nextOr(it3, NA) # list(2, 5, "versicolor")
#> [[1]]
#> [1] 2
#> 
#> [[2]]
#> [1] 5
#> 
#> [[3]]
#> [1] "versicolor"
#> 
nextOr(it3, NA) # list("w00t", 6, "virginica")
#> [[1]]
#> [1] "w00t"
#> 
#> [[2]]
#> [1] 6
#> 
#> [[3]]
#> [1] "virginica"
#> 
nextOr(it3, NA) # list("w00t", 7, "w00t")
#> [[1]]
#> [1] "w00t"
#> 
#> [[2]]
#> [1] 7
#> 
#> [[3]]
#> [1] "w00t"
#>