..or do I have to give
P.nk <- factorial(n) / factorial(n-k)
or
P.nk <- choose(n,k) * factorial(k)
Thank you.
..or do I have to give
P.nk <- factorial(n) / factorial(n-k)
or
P.nk <- choose(n,k) * factorial(k)
Thank you.
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.
exp(lfactorial(n) - lfactorial(n-k))
. But I will use second too. –
Bywoods I think the gregmisc package provides these functions.
library(gregmisc)
permutations(n=4,r=4)
Mailing list reference: [R] permutation
Check out nsamp(n,k,ordered=T)
in the 'prob' package
nsamp(500,2,ordered=TRUE)
encounter problem state in Rob answer (value out of range
). –
Bywoods [1] NaN Warning messages: 1: In factorial(n[i]) : value out of range in 'gammafn' 2: In factorial(n[i] - k[i]) : value out of range in 'gammafn'
> nsamp(171,2,ordered=TRUE) [1] Inf Warning message: In factorial(n[i]) : value out of range in 'gammafn'
> nsamp(170,2,ordered=TRUE) [1] 28730
----R version 3.5.3 –
Assiduity 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$$
package gtools
# R version 3.5.3
install.packages("gtools")
library(gtools)
base::nrow(gtools::permutations(500,2))
result:
[1] 249500
also see combinations-and-permutations-in-r, permutation_with_replacement.R
another package prob
:
base::ncol(prob::permsn(500,2))
[1] 249500
© 2022 - 2024 — McMap. All rights reserved.