How one can calculate the number of combinations and permutations in R?
The Combinations package failed to install on Linux with the following message:
> install.packages("Combinations") Installing package(s) into ‘/home/maxim/R/x86_64-pc-linux-gnu-library/2.13’ (as ‘lib’ is unspecified) Warning message: In getDependencies(pkgs, dependencies, available, lib) : package ‘Combinations’ is not available (for R version 2.13.1)
Combinat package in R programming language can be used to calculate permutations and combinations of the numbers. It provides routines and methods to perform combinatorics. combn() method in R language belonging to this package is used to generate all combinations of the elements of x taken m at a time.
The number of possible combinations is C(n,r)=n! r!
Use the following combination formula to calculate the value of nCr: nCr = n! / (r!
r = how many items are taken at a time. The ! symbol is a factorial, which is a number multiplied by all of the numbers before it. For example, 4!
The function combn is in the standard utils package (i.e. already installed)
choose is also already available in the Special {base}
If you don't want your code to depend on other packages, you can always just write these functions:
perm = function(n, x) { factorial(n) / factorial(n-x) } comb = function(n, x) { factorial(n) / factorial(n-x) / factorial(x) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With