如何在R中计算组合和排列?
如何计算 R 中组合和排列的数量?
组合包无法在 Linux 上安装,原因如下信息:
> install.packages("Combinations")
Installing package(s) into ‘/home/maxim/R/x86_64-pc-linux-gnu-library/2.13’
(as ‘lib’ is unspecified)
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
package ‘Combinations’ is not available (for R version 2.13.1)
How one can calculate the number of combinations and permutations in R?
The Combinations package failed to install on Linux with the following message:
> install.packages("Combinations")
Installing package(s) into ‘/home/maxim/R/x86_64-pc-linux-gnu-library/2.13’
(as ‘lib’ is unspecified)
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
package ‘Combinations’ is not available (for R version 2.13.1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
函数 combn 位于标准中utils 包(即已安装)
选择 也已经在特殊的{base}中可用
The function combn is in the standard utils package (i.e. already installed)
choose is also already available in the Special {base}
如果您不希望您的代码依赖于其他包,您可以随时编写以下函数:
If you don't want your code to depend on other packages, you can always just write these functions:
您可以将
combinat
包与 R 2.13 一起使用:如果您想知道组合/排列的数量,请检查结果的大小,例如:
You can use the
combinat
package with R 2.13:If you want to know the number of combination/permutations, then check the size of the result, e.g.:
Combinations 包不是标准 CRAN 包集的一部分,而是不同存储库 omegahat 的一部分。要安装它,您需要使用
请参阅 http://www.omegahat.org/Combinations/
The
Combinations
package is not part of the standard CRAN set of packages, but is rather part of a different repository, omegahat. To install it you need to useSee the documentation at http://www.omegahat.org/Combinations/
可能是“Combinations”包不再更新并且不能与最新版本的 R 一起使用(我也无法在 Windows 上的 R 2.13.1 上安装它)。
“combinat”包对我来说安装没有问题,并且可能是您的解决方案,具体取决于您到底想要做什么。
It might be that the package "Combinations" is not updated anymore and does not work with a recent version of R (I was also unable to install it on R 2.13.1 on windows).
The package "combinat" installs without problem for me and might be a solution for you depending on what exactly you're trying to do.
上面的几种解决方案涉及旨在列出实际组合的函数,然后应用长度函数来执行计算。如果你只想要数字,这是非常低效的。在我的系统中使用 rstudio.cloud 时,ncol(combn(1:70,5)) 大约需要 10 秒。 select(70,5) 花费的时间不到一秒。
正如 Marius Hofert 指出的那样,阶乘(500)这样的表达式存在数字问题。
在不使用非标准库的情况下,我相信选择(n,k)是组合的最佳解决方案。我所知道的最好的排列是选择(n,k)*阶乘(k),但如果有人知道获得排列的直接方法,请分享。
Several solutions above involve functions designed to list actual combinations, and then apply length function to perform the calculation. This is very inefficient if you just want the number. ncol(combn(1:70,5)) takes about 10 seconds using rstudio.cloud for my system. choose(70,5) takes less than a second.
As Marius Hofert pointed out there are issues numerically with expressions like factorial(500).
Without using a nonstandard library, I believe choose(n,k) is the best solution for combinations. The best I know for permutations is choose(n,k)*factorial(k), but please share if anyone knows a direct way to get the permutations.