[R] Efficient Cartesian product of data.frames

Gabor Grothendieck ggrothendieck at myway.com
Fri Sep 10 06:08:28 CEST 2004

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.

   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],]))

More information about the R-help mailing list