Example of permutations and combinations

Permutations and combinations are different ways of selecting a certain number of items from a set to create subsets. In both cases, we can allow item repetition or not. However, the difference between them is that the order of item selection is important for permutations, but not for combinations. Therefore, we end up with four different cases.

Example

example of permutations and combinations of a set of 5 items with a selection of 2 items

For example, the image above shows all the possible pairs of cell types selected from a set containing 5 cell types (neutrophils, monocytes, lymphocytes, eosinophils and basophils):

  • the 10 subsets written in black are present in the four cases
  • the 10 subsets written in purple are only present if order is important
  • the 5 subsets written in blue are only present if replacement is allowed

Number of permutations and combinations

If we define n as the number of items in the set and r as the number of items in the subsets, then we can count the number of possible subsets in each case with the following formulas:

  • number of permutations with repetition: n^r
  • number of permutations without repetition: \frac{n!}{(n-r)!}
  • number of combinations with repetition: \frac{(n+r-1)!}{r! (n-1)!}
  • number of combinations without repetition: \frac{n!}{r! (n-r)!}

Implementation in R

Finally, here is the implementation of the formulas in R (if n is not too big):

n = 5
r = 2

n_perm_rep = n^r
n_perm_no_rep = factorial(n) / factorial(n-r)
n_comb_rep = factorial(n+r-1) / (factorial(r) * factorial(n-1))
n_comb_no_rep = factorial(n) / (factorial(r) * factorial(n-r))

Conclusion

I hope that this first blog post was helpful for you!

Related posts

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply