expandGrid {RcppAlgos}R Documentation

Cartesian Product

Description

Generate the Cartesian Product of the input vectors. It is very similar to expand.grid however there are some notable differences:

Usage

    expandGrid(..., lower = NULL, upper = NULL,
               nThreads = NULL, return_df = FALSE)

Arguments

...

vectors, factors or a list containing these. (See ?expand.grid).

lower

The lower bound. Cartesian products are generated lexicographically, thus utilizing this argument will determine which specific product to start generating from (e.g. expandGrid(1:5, 3:11, lower = 6) is equivalent to expandGrid(1:5, 3:11)[6:expandGridCount(1:5, 3:11), ]). This argument along with upper is very useful for generating products in chunks allowing for easy parallelization.

upper

The upper bound. Similar to lower, however this parameter allows the user to stop generation at a specific product (e.g. expandGrid(1:5, 3:11, upper = 5) is equivalent to expandGrid(1:5, 3:11)[1:5, ])

nThreads

Specific number of threads to be used. The default is NULL.

return_df

Logical flag to force the output to be a data.frame. The default is FALSE.

Value

When all of the input is of the same type, by default expandGrid produces a matrix (a data.frame otherwise). This can be ignored by setting the argument return_df = TRUE.

Author(s)

Joseph Wood

See Also

comboGrid

Examples

## description example
lst = list(1:2, 1:2)

## Using base R
t = expand.grid(lst)

## vs using expandGrid. N.B. Output is a matrix
expandGrid(lst)

## Force a data.frame to be returned
expandGrid(lst, return_df = TRUE)

lst = Map(function(x, y) x:y, 8:14, 15:21)

## Use multiple threads for greater efficiency
system.time(expandGrid(lst, nThreads = 2))

[Package RcppAlgos version 2.9.3 Index]