Skip to Content

Combine every element with every other element of an object (cross-product)

I have two vectors of integers, say v1=c(1,2) and v2=c(3,4), I want to combine and obtain this as a result (as a data.frame, or matrix):

> combine(v1,v2) <--- doesn't exist
1 3
1 4
2 3
2 4

This is a basic case. What about a little bit more complicated - combine every row with every other row? E.g. imagine that we have two data.frames or matrices d1, and d2, and we want to combine them to obtain the following result:

d1
1 13
2 11
 
d2
3 12
4 10
 
> combine(d1,d2) <--- doesn't exist
1 13 3 12
1 13 4 10
2 11 3 12
2 11 4 10

How could I achieve this?