Not sure what this operation is called, and Google did not help.
Say I have two simple dataframes like this:
df1 <- data.frame(factor1 = c(\"a\", \"b
Here is another option with expand.grid
Map(expand.grid, factor1 = df1, factor2 = df2)$factor
# factor1 factor2
#1 a x
#2 b x
#3 c x
#4 a y
#5 b y
#6 c y
#7 a z
#8 b z
#9 c z
It's a cartesian product of the two data frames, when there's no common names, you can use merge
:
merge(df1, df2)
# factor1 factor2
#1 a x
#2 b x
#3 c x
#4 a y
#5 b y
#6 c y
#7 a z
#8 b z
#9 c z
Or more explicitly:
merge(df1, df2, by=c())
According to ?merge
, when there are no columns to join by, it returns a cartesian product of the two data frames:
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)).