Timothy W. Victor <tvictor <at> dolphin.upenn.edu> writes:
> I am looking for efficient code to produce the Cartesian product of two
> or more data.frames.
First create some test data consisting of a list of n=2 data frames.
data(iris)
L <- list(iris1 = iris[1:3,1:2], iris2 = iris[1:3,3:4])
Now calculate the cartesian product of the row indices, grid,
and, in the second line, cbind together the corresponding rows:
grid <- expand.grid(1:nrow(L[[1]]), 1:nrow(L[[2]]))
cbind(L[[1]][grid[,1],], L[[2]][grid[,2],])
Now generalize that to n >= 2 data frames:
grid <- do.call("expand.grid", lapply(L, function(x) 1:nrow(x)))
do.call("cbind", lapply(seq(L), function(i)L[[i]][grid[,i],]))