iproduct: Iterator that returns the Cartesian product of the arguments.

Description Usage Arguments Details Value Examples

View source: R/iproduct.r

Description

Constructs an iterator that is the Cartesian product of each of the arguments.

Usage

1

Arguments

...

multiple arguments

Details

Although they share the same end goal, iproduct can yield drastic memory savings compared to expand.grid.

Value

iterator that iterates through each element from the Cartesian product

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
it <- iproduct(x=1:3, y=4:5)
iterators::nextElem(it) # list(x=1, y=4)
iterators::nextElem(it) # list(x=1, y=5)
iterators::nextElem(it) # list(x=2, y=4)
iterators::nextElem(it) # list(x=2, y=5)
iterators::nextElem(it) # list(x=3, y=4)
iterators::nextElem(it) # list(x=3, y=5)

# iproduct is a replacement for base::expand.grid()
# Large data.frames are not created unless the iterator is manually consumed
a <- 1:2
b <- 3:4
c <- 5:6
it2 <- iproduct(a=a, b=b, c=c)
df_iproduct <- do.call(rbind, as.list(it2))
df_iproduct <- data.frame(df_iproduct)

# Compare df_iproduct with the results from base::expand.grid()
base::expand.grid(a=a, b=b, c=c)

itertools2 documentation built on May 30, 2017, 7:17 a.m.