當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。