How to calculate combination and permutation in R?
Asked Answered
S

6

41

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)
Slovenly answered 26/10, 2011 at 16:41 Comment(2)
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, C and other languages are not closed...Boydboyden
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.Boydboyden
M
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]
Melville answered 27/10, 2011 at 0:18 Comment(4)
Is there something that could just give me a number of possible combinations, rather then printing them all?Slovenly
@MaximVeksler Take a look at choose. (choose(5, 2) == ncol(combn(5, 2)))Seawards
This site can be used as alternate: englishact.com/Permutation/index.phpChoroid
Does not work with k = 0 or k = n. 5C0 or 5C5 should return 1 but it returns null.Offshoot
P
58

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

choose is also already available in the Special {base}

Plugboard answered 26/5, 2013 at 13:46 Comment(2)
wow alot of hassle for a simple question that its answer was 3 words "the choose function"Guggenheim
choose(n,k) = nCkGlaring
C
40

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)
}
Crum answered 21/7, 2013 at 18:24 Comment(1)
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...Kaylee
M
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]
Melville answered 27/10, 2011 at 0:18 Comment(4)
Is there something that could just give me a number of possible combinations, rather then printing them all?Slovenly
@MaximVeksler Take a look at choose. (choose(5, 2) == ncol(combn(5, 2)))Seawards
This site can be used as alternate: englishact.com/Permutation/index.phpChoroid
Does not work with k = 0 or k = n. 5C0 or 5C5 should return 1 but it returns null.Offshoot
M
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/

Medium answered 26/10, 2011 at 17:3 Comment(2)
This also does not work, I think that the R version I'm using is (2.13) is not compatibleSlovenly
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.Medium
B
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.

Bestride answered 26/10, 2011 at 16:57 Comment(0)
R
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.

Reinstate answered 3/3, 2021 at 22:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.