What is the choose() function in R?

Overview

The choose() function in R is used to calculate the number of sets with n elements that can be chosen from a set with k elements.

Mathematically:

choose (k, n) = n!k!(nk)!\frac{n!}{k!(n-k)!}

Note: n must always be greater than k.

Syntax

choose(k, n)

Parameters

The choose() function takes the following parameter values:

  • n: This represents the number of elements of a given set.
  • k: This represents the number of elements of another set.

Example

Let’s look at the code below:

# creating the number elements
n <- 3
# number of elements of a particular set
k <- 5
# use of choose() function to find how many times a set with 5 elements can be choosen from a set with 3 elements.
print(choose(k, n))

Explanation

  • Line 1: We obtain the number of elements.

  • Line 5: We obtain the number of elements of a particular set.

  • Line 8: We call the built-in function choose() with the parameters k and n.

Mathematically:

choose (n, k) = n!k!(nk)!\frac{n!}{k!(n-k)!}

choose (5, 3) = 5!3!(53)!\frac{5!}{3!(5-3)!} = 5!3!(2)!\frac{5!}{3!(2)!}= 10

Therefore, only 10 sets can be formed of a set with 5 elements chosen from another set having 3 elements.

Free Resources