如何在R中使用所有可能的算术函数获取矢量的所有可能组合?

huangapple go评论67阅读模式
英文:

How to get all possible combinations of a vector in R with all possible arithmetic functions?

问题

我想在R中对所有算术函数进行变量的排列组合。

testvector <- c("cat", "dog")

# 期望的结果

c("cat", "dog", "cat+dog", "cat-dog", "dog-cat", "cat*dog", "cat/dog", "dog/cat")

我有大约10个变量,所以手动操作很困难。我在Python中找到了一个可能能够完成相同任务的响应,但我必须在R中完成。

(https://stackoverflow.com/questions/61558074/how-to-perform-all-possible-combinations-of-arithmetic-operations-on-3-integers)

我想在一种手动的机器学习方法中使用这个,以找到分离数据集的最佳变量组合。

如果有一个ML包可以为我完成这个任务,我也很乐意尝试。

我还想在线性模型中使用这个向量的结果,但也许有一种更直接的方法。

另外,如果可能的话,我还想包括括号来分组变量

# 不完整的例子

testvector <- c("cat", "dog", "bird")

# 期望的结果

c("(cat-dog)/bird", "(bird+cat)/dog", "(dog+bird)*cat")
英文:

I would like to do a permutation of variables with all arithmetic functions in R.

example:

testvector &lt;- c(&quot;cat&quot;, &quot;dog&quot;)

# expected outcome

c(&quot;cat&quot;,&quot;dog&quot;,&quot;cat+dog&quot;,&quot;cat-dog&quot;,&quot;dog-cat&quot;,&quot;cat*dog&quot;,&quot;cat/dog&quot;,&quot;dog/cat&quot;)

I have approximately 10 variables, so this is difficult to do by hand. I found a response in python that can maybe do the same, but I have to do this in R.

( https://stackoverflow.com/questions/61558074/how-to-perform-all-possible-combinations-of-arithmetic-operations-on-3-integers )

I want to use this in kind of a manual machine learning approach to find the best combination of variables to separate a dataset.

If an ML package can do that for me, I'd also be happy to try it.

I also want to use the results of this vector in linear models, but maybe there is a more straight forward way.

One more thing, I also would like to - if possible - to include brackets to group variables

# incomplete example

testvector &lt;- c(&quot;cat&quot;,&quot;dog&quot;,&quot;bird&quot;)

# expected outcome looks like

c(&quot;(cat-dog)/bird&quot;,&quot;(bird+cat)/dog&quot;,&quot;(dog+bird)*cat&quot;)

It would be ok if this was not for character vectors, but for numeric and I would pre-calculate the necessary variables beforehand with the permutations and then use the results for modelling.

EDIT:

I edited the first comment to result in this (and it's a good start), but it would be nicer if
a) the length of the testvector could be incorporated so I do not have to adjust the formula by hand and
b) if "nonsensical" elements could be removed. The latter is not that important I think as I can program the loop to skip elements that do not result in a real number.


testvector &lt;- c(&quot;dog&quot;,&quot;cat&quot;,&quot;bird&quot;)

testvector &lt;- c(paste0(testvector,&quot;)&quot;), paste0(&quot;(&quot;,testvector), testvector)

c(testvector, do.call(paste, c(subset(expand.grid(testvector 
                                                  ,c(&quot;+&quot;, &quot;-&quot;, &quot;*&quot;, &quot;/&quot;)
                                                  , testvector
                                                  ,c(&quot;+&quot;, &quot;-&quot;, &quot;*&quot;, &quot;/&quot;)
                                                  , testvector)
                                      ), sep = &quot;&quot;)))

#head of output:

[1] &quot;dog)&quot;             &quot;cat)&quot;             &quot;bird)&quot;            &quot;(dog&quot;             &quot;(cat&quot;            
   [6] &quot;(bird&quot;            &quot;dog&quot;              &quot;cat&quot;              &quot;bird&quot;             &quot;cat)+dog)+dog)&quot;  
  [11] &quot;bird)+dog)+dog)&quot;  &quot;(dog+dog)+dog)&quot;   &quot;(cat+dog)+dog)&quot;   &quot;(bird+dog)+dog)&quot;  &quot;dog+dog)+dog)&quot;   
  [16] &quot;cat+dog)+dog)&quot;    &quot;bird+dog)+dog)&quot;   &quot;cat)-dog)+dog)&quot;   &quot;bird)-dog)+dog)&quot;  &quot;(dog-dog)+dog)&quot;  
  [21] &quot;(cat-dog)+dog)&quot;   &quot;(bird-dog)+dog)&quot;  &quot;dog-dog)+dog)&quot;    &quot;cat-dog)+dog)&quot;    &quot;bird-dog)+dog)&quot;  
  [26] &quot;cat)*dog)+dog)&quot;   &quot;bird)*dog)+dog)&quot;  &quot;(dog*dog)+dog)&quot;   &quot;(cat*dog)+dog)&quot;   &quot;(bird*dog)+dog)&quot; 
  [31] &quot;dog*dog)+dog)&quot;    &quot;cat*dog)+dog)&quot;    &quot;bird*dog)+dog)&quot;   &quot;cat)/dog)+dog)&quot;   &quot;bird)/dog)+dog)&quot; 


答案1

得分: 2

可以使用 expand.grid 获取组合,然后使用 paste 拼接行

c(测试向量,do.call(paste,c(subset(expand.grid(测试向量, 
  c(&quot;+&quot;, &quot;-&quot;, &quot;*&quot;, &quot;/&quot;), 测试向量), Var1 != Var3), sep = &quot;&quot;)))

输出

[1] &quot;猫&quot;     &quot;狗&quot;     &quot;狗+猫&quot; &quot;狗-猫&quot; &quot;狗*猫&quot; &quot;狗/猫&quot; &quot;猫+狗&quot; &quot;猫-狗&quot; &quot;猫*狗&quot; &quot;猫/狗&quot;
英文:

We could use expand.grid to get the combinations and then paste the rows

c(testvector, do.call(paste, c(subset(expand.grid(testvector, 
  c(&quot;+&quot;, &quot;-&quot;, &quot;*&quot;, &quot;/&quot;), testvector), Var1 != Var3), sep = &quot;&quot;)))

-output

[1] &quot;cat&quot;     &quot;dog&quot;     &quot;dog+cat&quot; &quot;dog-cat&quot; &quot;dog*cat&quot; &quot;dog/cat&quot; &quot;cat+dog&quot; &quot;cat-dog&quot; &quot;cat*dog&quot; &quot;cat/dog&quot;

</details>



huangapple
  • 本文由 发表于 2023年5月7日 13:26:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定