Is there an R function to get the number of permut

2020-06-16 03:38发布

..or do I have to give

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

or

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

Thank you.

4条回答
家丑人穷心不美
2楼-- · 2020-06-16 03:54

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

查看更多
仙女界的扛把子
3楼-- · 2020-06-16 04:03

I think the gregmisc package provides these functions.

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

Mailing list reference: [R] permutation

查看更多
Emotional °昔
4楼-- · 2020-06-16 04:14

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

查看更多
该账号已被封号
5楼-- · 2020-06-16 04:17

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.

查看更多
登录 后发表回答