views:

149

answers:

3

..or do I have to give

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

or

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

Thank you.

+4  A: 

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.

Rob Hyndman
First suggestion could be rewrite as `exp(lfactorial(n) - lfactorial(n-k))`. But I will use second too.
Marek
+1  A: 

I think the gregmisc package provides these functions.

library(gregmisc)
permutations(n=4,r=4)

Mailing list reference: [R] permutation

neilfws
Actually, what is asked would be given by: dim(permutations(n,k))[1]
gd047
A: 

Check out nsamp(n,k,ordered=T) in the 'prob' package

wkmor1
`nsamp(500,2,ordered=TRUE)` encounter problem state in Rob answer (`value out of range`).
Marek