Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 396
pms Avatar asked Feb 20 '23 06:02

pms


1 Answers

For the simple case of vectors there is expand.grid

v1 <- 1:2
v2 <- 3:4
expand.grid(v1, v2)
#  Var1 Var2
#1    1    3
#2    2    3
#3    1    4
#4    2    4

I don't know of a function that will automatically do what you want to do for dataframes(See edit)

We could relatively easily accomplish this using expand.grid and cbind.

df1 <- data.frame(a = 1:2, b=3:4)
df2 <- data.frame(cat = 5:6, dog = c("a","b"))

expand.grid(df1, df2) # doesn't work so let's try something else
id <- expand.grid(seq(nrow(df1)), seq(nrow(df2)))
out <-cbind(df1[id[,1],], df2[id[,2],])
out
#    a b cat dog
#1   1 3   5   a
#2   2 4   5   a
#1.1 1 3   6   b
#2.1 2 4   6   b

Edit: As Joran points out in the comments merge does this for us for data frames.

df1 <- data.frame(a = 1:2, b=3:4)
df2 <- data.frame(cat = 5:6, dog = c("a","b"))
merge(df1, df2)
#  a b cat dog
#1 1 3   5   a
#2 2 4   5   a
#3 1 3   6   b
#4 2 4   6   b
like image 134
Dason Avatar answered Feb 23 '23 15:02

Dason