英文:
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 <- c("cat", "dog")
# expected outcome
c("cat","dog","cat+dog","cat-dog","dog-cat","cat*dog","cat/dog","dog/cat")
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.
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 <- c("cat","dog","bird")
# expected outcome looks like
c("(cat-dog)/bird","(bird+cat)/dog","(dog+bird)*cat")
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 <- c("dog","cat","bird")
testvector <- c(paste0(testvector,")"), paste0("(",testvector), testvector)
c(testvector, do.call(paste, c(subset(expand.grid(testvector
,c("+", "-", "*", "/")
, testvector
,c("+", "-", "*", "/")
, testvector)
), sep = "")))
#head of output:
[1] "dog)" "cat)" "bird)" "(dog" "(cat"
[6] "(bird" "dog" "cat" "bird" "cat)+dog)+dog)"
[11] "bird)+dog)+dog)" "(dog+dog)+dog)" "(cat+dog)+dog)" "(bird+dog)+dog)" "dog+dog)+dog)"
[16] "cat+dog)+dog)" "bird+dog)+dog)" "cat)-dog)+dog)" "bird)-dog)+dog)" "(dog-dog)+dog)"
[21] "(cat-dog)+dog)" "(bird-dog)+dog)" "dog-dog)+dog)" "cat-dog)+dog)" "bird-dog)+dog)"
[26] "cat)*dog)+dog)" "bird)*dog)+dog)" "(dog*dog)+dog)" "(cat*dog)+dog)" "(bird*dog)+dog)"
[31] "dog*dog)+dog)" "cat*dog)+dog)" "bird*dog)+dog)" "cat)/dog)+dog)" "bird)/dog)+dog)"
答案1
得分: 2
可以使用 expand.grid
获取组合,然后使用 paste
拼接行
c(测试向量,do.call(paste,c(subset(expand.grid(测试向量,
c("+", "-", "*", "/"), 测试向量), Var1 != Var3), sep = "")))
输出
[1] "猫" "狗" "狗+猫" "狗-猫" "狗*猫" "狗/猫" "猫+狗" "猫-狗" "猫*狗" "猫/狗"
英文:
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("+", "-", "*", "/"), testvector), Var1 != Var3), sep = "")))
-output
[1] "cat" "dog" "dog+cat" "dog-cat" "dog*cat" "dog/cat" "cat+dog" "cat-dog" "cat*dog" "cat/dog"
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论