15.1 C
London
Friday, July 5, 2024
HomeRDescriptive Statistics in RHow to Calculate Combinations & Permutations in R

How to Calculate Combinations & Permutations in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following functions to calculate combinations and permutations in R:

#calculate total combinations of size r from n total objects
choose(n, r)

#calculate total permutations of size r from n total objects
choose(n, r) * factorial(r)

The following examples show how to use each of these functions in practice.

Example 1: Calculate Total Combinations

Combinations represent ways of selecting a sample from a group of objects in which the order of the objects does not matter.

For example, suppose we have a bag of four marbles: red, blue, green, and yellow. Suppose we’d like to select two marbles randomly from the bag, without replacement.

Here are the different combinations of marbles we could select:

  • {red, blue}
  • {red, green}
  • {red, yellow}
  • {blue, green}
  • {blue, yellow}
  • {green, yellow}

There are 6 total combinations.

Here is how to calculate the total number of combinations in R:

#calculate total combinations of size 2 from 4 total objects
choose(4, 2)

[1] 6

Our answer matches the number of combinations that we calculated by hand.

Example 2: Calculate Total Permutations

Permutations represent ways of selecting a sample from a group of objects in which the order of the objects does matter.

For example, suppose we have a bag of four marbles: red, blue, green, and yellow.

Suppose we’d like to select two marbles randomly from the bag, without replacement.

Here are the different permutations of marbles we could select:

  • {red, blue}, {blue, red}
  • {red, green}, {green, red}
  • {red, yellow}, {yellow, red}
  • {blue, green}, {green, blue}
  • {blue, yellow}, {yellow, blue}
  • {green, yellow}, {yellow, green}

There are 12 total permutations.

Here is how to calculate the total number of permutations in R:

#calculate total permutations of size 2 from 4 total objects
choose(4, 2) * factorial(2)

[1] 12

Our answer matches the number of permutations that we calculated by hand.

Additional Resources

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

How to Perform Linear Interpolation in R (With Example)
How to Select Unique Rows in a Data Frame in R
How to Replicate Rows in Data Frame in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories