40

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)
krassowski
  • 13,598
  • 4
  • 60
  • 92
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151
  • 1
    Why was this closed? It is not asking for a recommendation about a book or software library, but about calculation of combinatoric function in a specific language. Equivalent question for [Python](https://stackoverflow.com/questions/3025162/statistics-combinations-in-python), [C](https://stackoverflow.com/questions/1838368/calculating-the-amount-of-combinations) and other languages are not closed... – krassowski Jan 04 '21 at 16:25
  • 1
    I edited the question to remove the mention of software recommendation. Given the quality answers (and useful discussions in comments) I believe this question should be re-opened. – krassowski Jan 04 '21 at 16:32

6 Answers6

54

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

choose is also already available in the Special {base}

PeterVermont
  • 1,922
  • 23
  • 18
41

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)
}
ctbrown
  • 2,271
  • 17
  • 24
CCC
  • 891
  • 8
  • 9
  • 28
    That's a bad idea numerically. R can evaluate `choose(500, 2)` but not `factorial(500)`. You should at least work with `lfactorial` and then take `exp()`. The only reason I'm posting this is that your answer has so many upvotes, it seems people don't know these things... – Marius Hofert Nov 14 '17 at 13:01
35

You can use the combinat package with R 2.13:

install.packages("combinat")
require(combinat)
permn(3)
combn(3, 2)

If you want to know the number of combination/permutations, then check the size of the result, e.g.:

length(permn(3))
dim(combn(3,2))[2]
JPalma
  • 3
  • 2
Shane
  • 98,550
  • 35
  • 224
  • 217
3

The Combinations package is not part of the standard CRAN set of packages, but is rather part of a different repository, omegahat. To install it you need to use

install.packages("Combinations", repos = "http://www.omegahat.org/R")

See the documentation at http://www.omegahat.org/Combinations/

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
  • This also does not work, I think that the R version I'm using is (2.13) is not compatible – Maxim Veksler Oct 27 '11 at 11:47
  • Ah, there was a typo; it should be omegahat, not omeghat. I copied and pasted the command, but I should have tested it first. I've updated my answer. This updated code works for me in 2.13.2 on Windows. – Brian Diggs Oct 27 '11 at 14:18
1

It might be that the package "Combinations" is not updated anymore and does not work with a recent version of R (I was also unable to install it on R 2.13.1 on windows). The package "combinat" installs without problem for me and might be a solution for you depending on what exactly you're trying to do.

Antoine Vernet
  • 276
  • 5
  • 7
0

Several solutions above involve functions designed to list actual combinations, and then apply length function to perform the calculation. This is very inefficient if you just want the number. ncol(combn(1:70,5)) takes about 10 seconds using rstudio.cloud for my system. choose(70,5) takes less than a second.

As Marius Hofert pointed out there are issues numerically with expressions like factorial(500).

Without using a nonstandard library, I believe choose(n,k) is the best solution for combinations. The best I know for permutations is choose(n,k)*factorial(k), but please share if anyone knows a direct way to get the permutations.