在 R 编程中生成 xCm 的所有组合 – combn()函数
R 语言中的combn()
函数用于生成一次取m的x元素的所有组合。
Syntax: combn(x, m)
Parameters:
x: total number of elements taken
r: number of elements taken at a time out of “x” elements
示例 1:
# R program to illustrate
# combn function
# Calling the combn() function
combn(5, 3)
combn(6, 5)
combn(2, 2)
输出:
[, 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
[, 1] [, 2] [, 3] [, 4] [, 5] [, 6]
[1, ] 1 1 1 1 1 2
[2, ] 2 2 2 2 3 3
[3, ] 3 3 3 4 4 4
[4, ] 4 4 5 5 5 5
[5, ] 5 6 6 6 6 6
[, 1]
[1, ] 1
[2, ] 2
示例 2:
# R program to illustrate
# combn function
# Calling the combn() function
combn(LETTERS[1: 4], 2)
combn(LETTERS[2: 6], 3)
输出:
[, 1] [, 2] [, 3] [, 4] [, 5] [, 6]
[1, ] "A" "A" "A" "B" "B" "C"
[2, ] "B" "C" "D" "C" "D" "D"
[, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [, 7] [, 8] [, 9] [, 10]
[1, ] "B" "B" "B" "B" "B" "B" "C" "C" "C" "D"
[2, ] "C" "C" "C" "D" "D" "E" "D" "D" "E" "E"
[3, ] "D" "E" "F" "E" "F" "F" "E" "F" "F" "F"