当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 combn()用法及代码示例


R语言中的combn()函数用于生成一次取m个x的元素的所有组合。

用法: combn(x, m)

参数:
x:获取的元素总数
r:“x” 个元素中一次取出的元素数

范例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"  

相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Generate all Combinations of xCm in R Programming – combn() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。