R all combinations of 3 vectors with conditions

I have two vectors

vector1 = c(0.9,0.8,0.7,0.6,0.5)
vector2 = c(10,20,30)

      

Now I want all combinations of elements in these vectors, and is vector2

used twice. I am using expand.grid()

for this.

combinations = expand.grid(vector1,vector2,vector2)

      

The result is a frame with columns Var1

, Var2

and Var3

.

Now I want to combine the first vector with the second vector with some conditions. For example. 0.9-0.7 from vector1

should only be combined with Var2 >= Var3

. And from 0.6 to 0.5 should only be combined with Var2 <= Var3

.

How can i do this?

This is an example. The real number of combinations is about 18,000 elements with 3 decimal places. So I am also looking for an efficient way.

0


source to share


1 answer


Why not just create a grid and then a subset. For example,



co = expand.grid(vector1,vector2,vector2)
subset(co, (Var1 >= 0.7 & Var1 <= 0.9) & Var2 >= Var3  )

      

+2


source







All Articles