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

余生长醉 提交于 2019-12-13 11:42:12

问题


..or do I have to give

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

or

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

Thank you.


回答1:


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.




回答2:


I think the gregmisc package provides these functions.

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

Mailing list reference: [R] permutation




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/2871763/is-there-an-r-function-to-get-the-number-of-permutations-of-n-objects-take-k-pn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!