Question

I have two vectors

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

I now want all combinations of the elements in these vectors, while vector2 is used twice. I use expand.grid() to this.

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

The result is a frame with the columns Var1, Var2 and Var3.

Now I want to combine the first vector with the second vector with some conditions. E.g. 0.9 to 0.7 from vector1 should only be combined with Var2 >= Var3. And 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 decimals. So I am also looking for an efficient way.

Was it helpful?

Solution

Why not just generate your grid, then subset. For example,

co = expand.grid(vector1,vector2,vector2)
subset(co, (Var1 >= 0.7 & Var1 <= 0.9) & Var2 >= Var3  )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top