Learn R Programming

vectools (version 0.3.0)

60_cartesian_products: Cartesian Product

Description

Computes the cartesian product of one or more sets.

By default, a matrix is returned, if possible. Each row, represents one combination.

In some cases, as.list (to return lists) needs to be set to true. (If there's a lack of supporting methods).

Usage

cprod (…, sets = list(…), gby.last=FALSE, as.list=FALSE)

Arguments

sets

List containing one or more vectors. Note that standard vectors can be replaced with other objects. However, each object requires a rep method with an each and times argument, and you need to set as.list to true.

gby.last

Logical, if false (default), group by the first set, then the second, and so on. If true, group by the last set, then the second to last, and so on.

as.list

Logical, if true, return a list, rather than a matrix. Each element corresponds to one column from the corresponding matrix.

Argument list containing one or more standard vectors. Often, easier way to specify sets, above.

Examples

Run this code
# NOT RUN {
cprod (1:2, 1:4)
cprod (1:2, 1:4, gby.last=TRUE)
# }

Run the code above in your browser using DataLab