R: any function for the Cartesian product of two data frames?

I need to make the Cartesian product of two data frames. For instance,

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

then C would be put after the 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

since some identifiers are the same in A, so I cannot use a method like

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

This method will generate more lines. Can someone help me get out of here? Thanks

+4
source share
1 answer

merge(A, B)if there are no columns linking the two, should this be done by default, no?

From ?merge(my attention):

by by.x by.y 0 ( NULL), r < > x y, .. dim (r) = c (nrow (x) * nrow (y), ncol (x) + ncol (y)).

, , ?merge. R ; rseek .

+8

Source: https://habr.com/ru/post/1652789/


All Articles