如果想要提取vector里面的3个元素的所有可能组合,怎么做?
1) r basic
combn function - RDocumentation
2) combinations from gtools
combinations function - RDocumentation
example:
- example <- c("zhao","qian","sun", "li", "zhou", "wu")
-
- combn(example, 2)
- [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
- [1,] "zhao" "zhao" "zhao" "zhao" "zhao" "qian" "qian" "qian" "qian" "sun"
- [2,] "qian" "sun" "li" "zhou" "wu" "sun" "li" "zhou" "wu" "li"
- [,11] [,12] [,13] [,14] [,15]
- [1,] "sun" "sun" "li" "li" "zhou"
- [2,] "zhou" "wu" "zhou" "wu" "wu"
-
- gtools::combinations(length(example), 2, example)
- [,1] [,2]
- [1,] "li" "qian"
- [2,] "li" "sun"
- [3,] "li" "wu"
- [4,] "li" "zhao"
- [5,] "li" "zhou"
- [6,] "qian" "sun"
- [7,] "qian" "wu"
- [8,] "qian" "zhao"
- [9,] "qian" "zhou"
- [10,] "sun" "wu"
- [11,] "sun" "zhao"
- [12,] "sun" "zhou"
- [13,] "wu" "zhao"
- [14,] "wu" "zhou"
- [15,] "zhao" "zhou"