要创建矢量元素的三元组,可以使用combn函数。例如,如果我们有一个向量x包含值1,2,3,4,5,则可以使用combn(x,3)创建x的无序三元组。这将创建一个矩阵,其中向量x的元素将不按特定顺序排列。
x1<-1:5 combn(x1,3)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 1 1 1 1 1 2 2 2 3 [2,] 2 2 2 3 3 4 3 3 4 4 [3,] 3 4 5 4 5 5 4 5 5 5
x2<-1:8 combn(x2,3)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [2,] 2 2 2 2 2 2 3 3 3 3 3 4 4 4 [3,] 3 4 5 6 7 8 4 5 6 7 8 5 6 7 [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [1,] 1 1 1 1 1 1 1 2 2 2 2 2 [2,] 4 5 5 5 6 6 7 3 3 3 3 3 [3,] 8 6 7 8 7 8 8 4 5 6 7 8 [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [1,] 2 2 2 2 2 2 2 2 2 2 3 3 [2,] 4 4 4 4 5 5 5 6 6 7 4 4 [3,] 5 6 7 8 6 7 8 7 8 8 5 6 [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50] [1,] 3 3 3 3 3 3 3 3 4 4 4 4 [2,] 4 4 5 5 5 6 6 7 5 5 5 6 [3,] 7 8 6 7 8 7 8 8 6 7 8 7 [,51] [,52] [,53] [,54] [,55] [,56] [1,] 4 4 5 5 5 6 [2,] 6 7 6 6 7 7 [3,] 8 8 7 8 8 8
x3<-0:5 combn(x3,3)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 [2,] 1 1 1 1 2 2 2 3 3 4 2 2 2 3 [3,] 2 3 4 5 3 4 5 4 5 5 3 4 5 4 [,15] [,16] [,17] [,18] [,19] [,20] [1,] 1 1 2 2 2 3 [2,] 3 4 3 3 4 4 [3,] 5 5 4 5 5 5
x4<-rpois(5,1) x4
输出结果
[1] 5 2 1 1 0
combn(x4,3)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 5 5 5 5 5 5 2 2 2 1 [2,] 2 2 2 1 1 1 1 1 1 1 [3,] 1 1 0 1 0 0 1 0 0 0
combn(x5,3)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 8 8 8 8 8 8 8 8 8 8 8 8 8 8 [2,] 0 0 0 0 0 0 7 7 7 7 7 5 5 5 [3,] 7 5 4 7 7 8 5 4 7 7 8 4 7 7 [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [1,] 8 8 8 8 8 8 8 0 0 0 0 0 [2,] 5 4 4 4 7 7 7 7 7 7 7 7 [3,] 8 7 7 8 7 8 8 5 4 7 7 8 [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [1,] 0 0 0 0 0 0 0 0 0 0 7 7 [2,] 5 5 5 5 4 4 4 7 7 7 5 5 [3,] 4 7 7 8 7 7 8 7 8 8 4 7 [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50] [1,] 7 7 7 7 7 7 7 7 5 5 5 5 [2,] 5 5 4 4 4 7 7 7 4 4 4 7 [3,] 7 8 7 7 8 7 8 8 7 7 8 7 [,51] [,52] [,53] [,54] [,55] [,56] [1,] 5 5 4 4 4 7 [2,] 7 7 7 7 7 7 [3,] 8 8 7 8 8 8
x6<-sample(11:15,7,replace=TRUE) x6
输出结果
[1] 14 12 11 12 12 13 15
combn(x6,3)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 14 14 14 14 14 14 14 14 14 14 14 14 14 14 [2,] 12 12 12 12 12 11 11 11 11 12 12 12 12 12 [3,] 11 12 12 13 15 12 12 13 15 12 13 15 13 15 [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [1,] 14 12 12 12 12 12 12 12 12 12 12 11 [2,] 13 11 11 11 11 12 12 12 12 12 13 12 [3,] 15 12 12 13 15 12 13 15 13 15 15 12 [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [1,] 11 11 11 11 11 12 12 12 12 [2,] 12 12 12 12 13 12 12 13 13 [3,] 13 15 13 15 15 13 15 15 15
x7<-rpois(8,20) x7
输出结果
[1] 9 18 29 27 15 21 21 17
combn(x7,3)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 [2,] 18 18 18 18 18 18 29 29 29 29 29 27 27 27 [3,] 29 27 15 21 21 17 27 15 21 21 17 15 21 21 [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [1,] 9 9 9 9 9 9 9 18 18 18 18 18 [2,] 27 15 15 15 21 21 21 29 29 29 29 29 [3,] 17 21 21 1 7 21 17 17 27 15 21 21 17 [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38] [1,] 18 18 18 18 18 18 18 18 18 18 29 29 [2,] 27 27 27 27 15 15 15 21 21 21 27 27 [3,] 15 21 21 17 21 21 17 21 17 17 15 21 [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50] [1,] 29 29 29 29 29 29 29 29 27 27 27 27 [2,] 27 27 15 15 15 21 21 21 15 15 15 21 [3,] 21 17 21 2 1 17 21 17 17 21 21 17 21 [,51] [,52] [,53] [,54] [,55] [,56] [1,] 27 27 15 15 15 21 [2,] 21 21 21 21 21 21 [3,] 17 17 21 17 17 17