R带条件的3个向量的所有组合 [英] R all combinations of 3 vectors with conditions

查看:98
本文介绍了R带条件的3个向量的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个向量

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

我现在想要这些向量中元素的所有组合,而 vector2 被使用了两次。我对此使用 expand.grid()

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)

结果是一个框架,其列为 Var1 Var2 Var3

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

现在我想在某些条件下将第一个向量与第二个向量组合。
例如从 vector1 到0.9到0.7只能与 Var2> = Var3 结合使用。并且0.6到0.5仅应与 Var2< = 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.

我该怎么做?

这是一个示例。组合的实际数量约为18,000个元素,带有3个小数。因此,我也在寻找一种有效的方法。

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.

推荐答案

为什么不仅要生成网格,还要生成子集。例如,

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  )

这篇关于R带条件的3个向量的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆