To calculate combination use choose() function and to calculate permutation use factorial() function in R. Combinations and permutations are used to count the number of ways in which objects can be selected from a set or arranged in a specific order.
The following methods show how you can use it with syntax.
Method 1: Use choose() Function
choose(x,y)
x: The total number of items y: The number of items to choose
Method 2: Use factorial() Function
choose(x, y) * factorial(y)
The following examples shows how to use choose() and factorial() function in R.
Calculate Combinations and Permutations Using choose() and factorial() Function in R
Let’s see how we can calculate combinations and permutations in R:
# Calculate Combinations
c <- choose(8,5)
# Calculate Permutations
p <- choose(8,5) * factorial(2)
# Show Combinations
print(paste("Combinations:", c))
# Show Permutations
print(paste("Permutations:", p))
Output:
[1] "Combinations: 56"
[1] "Permutations: 112"
The output show the number of ways to choose 5 items from a set of 8.