8

I need to do a cartesian product of two data frames. For example,

 A = id weight type
     10    20     a
     10    30     b
     25    10     c
 B = date  report
     2007    y
     2008    n

then C would be like after doing cartesian product of A and B

 C =  id weight type  date  report
      10    20     a    2007    y
      10    20     a    2008    n
      10    30     b    2007    y
      10    30     b    2008    n
      25    10     c    2007    y
      25    10     c    2008    n

as some ids are the same in A, so I cannot use a way like

C <- merge(A$id,B$date)
C <- merge(C,A,by="id")
C <- merge(C,B,by="date")

This way will generate more rows. Could anyone help me out of here? Thanks

Feng Chen
  • 2,139
  • 4
  • 33
  • 62

1 Answers1

14

merge(A, B), provided there are no columns linking the two, should do this by default, no?

From ?merge (emphasis mine):

If by or both by.x and by.y are of length 0 (a length zero vector or NULL), the result, r, is the Cartesian product of x and y, i.e., dim(r) = c(nrow(x)*nrow(y), ncol(x) + ncol(y)).

Admittedly, this does require one to know to look in ?merge. Context-based searching in R is severely lacking; even rseek doesn't immediately provide this.

Jonathan Carroll
  • 3,897
  • 14
  • 34