cprod {lazy.tools}R Documentation

Cartesian product of many vectors and matrices

Description

Cartesian product of many vectors and matrices

Usage

cprod(..., rev = 1, as.data.frame = 0, rownames = 0, colnames = 0)

Arguments

...

vectors, factors or matrices
Or ... can be a list containing the levels vectors as its elements.

rev

= 0 to make the fist elements of ... change first

as.data.frame

= 1 to make the result a data frame.

rownames

= 1 to return the compressed version as the rownames.

colnames

= 1 to use the names of ... as the colnames.

Details

When all the inputs are vectors, cprod is equivalent to expand.grid1.

When both A and B are matrices, cprod( A, B ) is equivalent to

  Reduce( rbind, lapply(as.data.frame(t(A))
       ,  function(x) cbind(repmat(matrix(x,1,ncol(A)),nrow(B)),B)) )

Value

A matrix or data frame.

Examples

cprod(1:2,3:5)
cprod(1:2,3:4, rev=0)
cprod(matrix(1:6,3,2, byrow=1),1:2)
cprod(matrix(1:6,3,2, byrow=1),matrix(1:4,2,2,byrow=1))

# when ... is a list
ll <- list(a=c("a","b"),bbb=1:3)
cprod(ll, colnames=0)
cprod(ll, colnames=1)

# ANOVA data
levels <- c(2,3)
ll <- lapply(levels,function(x)seq(1:x))
names(ll) <- c("A","B")
data <- cprod(ll, colnames=1)

# generate 3D grid of x0
x0 <- 1:3
ndim <- 3
x <- cprod( rep(list(x0),ndim) )
Print(x)


[Package lazy.tools version 0.1.6 ]