获取R中的所有组合,允许重复 [英] Getting all combinations in R, repetition allowed
本文介绍了获取R中的所有组合,允许重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
内置的 combn
仅给出一半的组合:
The built-in combn
only gives half the combinations:
> t(combn(1:5, 2))
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 1 4
[4,] 1 5
[5,] 2 3
[6,] 2 4
[7,] 2 5
[8,] 3 4
[9,] 3 5
[10,] 4 5
例如,既没有(1,1)也没有(2,1)。
For example there is no (1,1) nor (2,1).
如何获得所有组合?
推荐答案
正如@akrun所说, expand.grid
As @akrun said, it looks like expand.grid
will do it.
> expand.grid(rep(list(1:5), 2))
Var1 Var2
1 1 1
2 2 1
3 3 1
4 4 1
5 5 1
6 1 2
7 2 2
8 3 2
9 4 2
10 5 2
11 1 3
12 2 3
13 3 3
14 4 3
15 5 3
16 1 4
17 2 4
18 3 4
19 4 4
20 5 4
21 1 5
22 2 5
23 3 5
24 4 5
25 5 5
这篇关于获取R中的所有组合,允许重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文