Let's suppose I have two vectors of different length x_1 and y_1 as follows:
x_1 = seq(0,49,2)
y_1 = seq(-90,-51,2)
Now I want to find all the possible combinations, with size = 5, of these two vectors and therefore I do:
library(tidyr)
x_all = data.frame(t(rbind(combn(x_1, 5))))
y_all = data.frame(t(rbind(combn(y_1, 5))))
Now I want to keep only the combinations with values progressing by a step of 2:
x_all = x_all[x_all$X2 - x_all$X1 == 2 &
x_all$X3 - x_all$X2 == 2 &
x_all$X4 - x_all$X3 == 2 &
x_all$X5 - x_all$X4 == 2, ]
y_all = y_all[y_all$X2 - y_all$X1 == 2 &
y_all$X3 - y_all$X2 == 2 &
y_all$X4 - y_all$X3 == 2 &
y_all$X5 - y_all$X4 == 2, ]
And now my question:
How can I merge x_all and y_all so that I get a unique data.frame with all the possible combinations between these two sets?
Any suggestions?
Here what I tried without success:
# Assign an index to each set
x_all$index = c(1:nrow(x_all))
y_all$index = c(1:nrow(y_all))
# Merge the sets
x_y_all = merge(x_all, y_all, by = 'index', all = TRUE)
The output should look something like this:
X1.x X2.x X3.x X4.x X5.x X1.y X2.y X3.y X4.y X5.y
0 2 4 6 8 -90 -88 -86 -84 -82
0 2 4 6 8 -88 -86 -84 -82 -80
0 2 4 6 8 -86 -84 -82 -80 -78
....
2 4 6 8 10 -90 -88 -86 -84 -82
2 4 6 8 10 -88 -86 -84 -82 -80
2 4 6 8 10 -86 -84 -82 -80 -78
....