Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate combination and permutation in R?

Tags:

r

combinations

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) 
like image 502
Maxim Veksler Avatar asked Oct 26 '11 16:10

Maxim Veksler


People also ask

How do you find permutations and combinations in r?

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.

How do you find the number of combinations in r?

The number of possible combinations is C(n,r)=n! r!

How do you calculate nCr in r?

Use the following combination formula to calculate the value of nCr: nCr = n! / (r!

What is the r value in permutation?

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!


2 Answers

The function combn is in the standard utils package (i.e. already installed)

choose is also already available in the Special {base}

like image 77
PeterVermont Avatar answered Oct 07 '22 22:10

PeterVermont


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) } 
like image 30
CCC Avatar answered Oct 07 '22 22:10

CCC