dataframe - R: any function for Cartesian Product of two data frames? -
this question has answer here:
- how cross join in r? 6 answers
i need cartesian product of 2 data frames. example,
= id weight type 10 20 10 30 b 25 10 c b = date report 2007 y 2008 n
then c after doing cartesian product of , b
c = id weight type date report 10 20 2007 y 10 20 2008 n 10 30 b 2007 y 10 30 b 2008 n 25 10 c 2007 y 25 10 c 2008 n
as ids same in a, cannot use way
c <- merge(a$id,b$date) c <- merge(c,a,by="id") c <- merge(c,b,by="date")
this way generate more rows. me out of here? thanks
merge(a, b)
, provided there no columns linking two, should default, no?
from ?merge
(emphasis mine):
if or both by.x , by.y of length 0 (a length 0 vector or null), result, r, cartesian product of x , y, i.e., dim(r) = c(nrow(x)*nrow(y), ncol(x) + ncol(y)).
admittedly, require 1 know in ?merge
. context-based searching in r severely lacking; rseek doesn't provide this.
Comments
Post a Comment