Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an R function to get the number of permutations of n objects take k P(n,k)?

..or do I have to give

P.nk <- factorial(n) / factorial(n-k)

or

P.nk <- choose(n,k) * factorial(k)

Thank you.

like image 602
Brani Avatar asked May 20 '10 07:05

Brani


People also ask

How do we find the number of permutations of n objects taken r at a time?

The number of permutations of n objects taken r at a time is determined by the following formula: P(n,r)=n! (n−r)!

Is there a permutation function in r?

permn() method in R generates all permutations of the elements of x. If x is a positive integer, returns all permutations of the elements of seq(x). The permutation of the number 0 is 1.

How do you find the number of combinations in r?

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


2 Answers

Another way for doing that, from Base R is

permn <- prod( (n-(0:(k-1)))

that is a simple implementation of the following formula

$$p(n,k) = \prod_{j=0}^{k-1} n-j$$

like image 176
JDG Avatar answered Oct 14 '22 08:10

JDG


I don't know of any existing function. Your first suggestion will fail with large n. Your second idea should work fine when written as a function:

perm <- function(n,k){choose(n,k) * factorial(k)}

Then perm(500,2) will give 249500 for example.

like image 23
Rob Hyndman Avatar answered Oct 14 '22 08:10

Rob Hyndman