- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create unordered triplets of a vector elements in R?
To create triplets of a vector elements, we can use combn function. For example, if we have a vector x that contain values 1, 2, 3, 4, 5 then the unordered triplets of x can be created by using combn(x,3). This will create a matrix where the elements of the vector x would not be arranged in a particular order.
Example
x1<-1:5 combn(x1,3)
Output
[,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
Example
x2<-1:8 combn(x2,3)
Output
[,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
Example
x3<-0:5 combn(x3,3)
Output
[,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
Example
x4<-rpois(5,1) x4
Output
[1] 5 2 1 1 0
Example
combn(x4,3)
Output
[,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
Example
combn(x5,3)
Output
[,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
Example
x6<-sample(11:15,7,replace=TRUE) x6
Output
[1] 14 12 11 12 12 13 15
Example
combn(x6,3)
Output
[,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
Example
x7<-rpois(8,20) x7
Output
[1] 9 18 29 27 15 21 21 17
Example
combn(x7,3)
Output
[,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
Advertisements