How to Use the choose() Function in R


Often you may want to calculate the number of sets with k elements that can be chosen from a set of k total elements.

Written in mathematical terms, we can express this as:

  • choose(n, k) = n! / (k!(n-k)!)

Note that the ! symbol is used to represent a factorial in mathematics.

One of the easiest ways to do this in R is by using the choose() function, which can be used to perform this exact task.

The choose() function uses the following syntax:

choose(n, k)

where:

  • n: Number of total elements in one set
  • k: Number of elements in a given set you would like to choose

The following example shows how to use the choose() function in practice.

Note: The choose() function is available in base R so you do not need to install any external packages to utilize this function.

Example: How to Use the choose() Function in R

Suppose that we would like to know the number of different ways that 2 elements can be chosen from a set with 5 elements.

We can use the choose() function with the following syntax to answer this question:

#find number of ways that 2 elements can be chosen from set of 5 elements
choose(5, 2)

[1] 10

The choose() function returns a value of 10, which tells us that there are 10 ways that 2 elements can be chosen from a set with 5 elements.

We can verify that this is correct by manually calculating the answer using the formula that we shared earlier:

  • choose(n, k) = n! / (k!(n-k)!)
  • choose(5, 2) = 5! / (2!(5-2)!)
  • choose(5, 2) = 120 / (2(6))
  • choose(5, 2) = 120 / 12
  • choose(5, 2) = 10

This matches the value returned by the choose() function in R.

Feel free to use different values for n and k as well.

For example, suppose that we would like to know the number of different ways that 4 elements can be chosen from a set with 7 elements.

We can use the choose() function with the following syntax to answer this question:

#find number of ways that 4 elements can be chosen from set of 7 elements
choose(7, 4)

[1] 35

The choose() function returns a value of 35, which tells us that there are 35ways that 4 elements can be chosen from a set with 7 elements.

We can verify that this is correct by manually calculating the answer using the formula that we shared earlier:

  • choose(n, k) = n! / (k!(n-k)!)
  • choose(7, 4) = 7! / (4!(7-4)!)
  • choose(7, 4) = 5040 / (24(6))
  • choose(7, 4) = 5040 / 144
  • choose(7, 4) = 35

This matches the value returned by the choose() function in R.

It’s important to note that the value of n must always be greater than the value for k. This should make sense mathematically because it’s not possible for us to choose, say, 7 elements, from a list of only 4 elements.

If we attempt to use a value for n that is less than k in the choose() function in R then the result will simply be zero.

For example, suppose that we attempt to use the following syntax:

#find number of ways that74 elements can be chosen from set of 4 elements
choose(4, 7)

[1] 0

The choose() function returns zero since there are zero ways to choose 7 elements from a list of only 4 elements.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Create a Frequency Table by Group in R
How to Create Relative Frequency Tables in R
How to Use the describe() Function in R
How to Use the describeBy() Function in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *