Calculate nCr value in R Programming – choose() Function

R Language offers a direct function that can compute the nCr value without writing the whole code for computing nCr value.

Syntax: choose(n, r)

Parameters:
n: Number of elements
r: Number of combinations

Returns: The number of r combinations from a total of n elements, i.e, nCr value.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to calculate nCr value 
    
# Using choose() method 
answer1 <- choose(3, 2
answer2 <- choose(3, 7)  
answer3 <- choose(7, 3)
  
print(answer1) 
print(answer2) 
print(answer3) 

chevron_right


Output:

3
0
35

Example 2: If we provide the value of n and r such that n < r then choose(n, r) will return 0.

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to calculate nCr value 
     
# Using choose() method 
answer1 <- choose(2, 3
answer2 <- choose(3, 6)  
answer3 <- choose(3, 7)
   
print(answer1) 
print(answer2) 
print(answer3) 

chevron_right


Output:

0
0
0
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.