Generate all Combinations of xCm in R Programming – combn() Function
Last Updated :
08 Jun, 2020
combn()
function in R Language is used to generate all combinations of the elements of x taken m at a time.
Syntax: combn(x, m)
Parameters:
x: total number of elements taken
r: number of elements taken at a time out of “x” elements
Example 1:
combn( 5 , 3 )
combn( 6 , 5 )
combn( 2 , 2 )
|
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
[, 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
Example 2:
combn(LETTERS[ 1 : 4 ], 2 )
combn(LETTERS[ 2 : 6 ], 3 )
|
Output:
[, 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"
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...