R: any function for Cartesian Product of two data frames? [duplicate]
Asked Answered
C

1

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

Counterblast answered 29/8, 2016 at 3:23 Comment(0)
B
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.

Brownout answered 29/8, 2016 at 3:28 Comment(1)
you are right. thanks a lot!!Counterblast

© 2022 - 2024 — McMap. All rights reserved.