如何在R中计算组合和排列?

发布于 2024-12-11 22:42:42 字数 459 浏览 0 评论 0原文

如何计算 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时光匆匆的小流年 2024-12-18 22:42:42

函数 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}

笑忘罢 2024-12-18 22:42:42

如果您不希望您的代码依赖于其他包,您可以随时编写以下函数:

perm = function(n, x) {
  factorial(n) / factorial(n-x)
}

comb = function(n, x) {
  factorial(n) / factorial(n-x) / factorial(x)
}

If you don't want your code to depend on other packages, you can always just write these functions:

perm = function(n, x) {
  factorial(n) / factorial(n-x)
}

comb = function(n, x) {
  factorial(n) / factorial(n-x) / factorial(x)
}
请叫√我孤独 2024-12-18 22:42:42

您可以将 combinat 包与 R 2.13 一起使用:

install.packages("combinat")
require(combinat)
permn(3)
combn(3, 2)

如果您想知道组合/排列的数量,请检查结果的大小,例如:

length(permn(3))
dim(combn(3,2))[2]

You can use the combinat package with R 2.13:

install.packages("combinat")
require(combinat)
permn(3)
combn(3, 2)

If you want to know the number of combination/permutations, then check the size of the result, e.g.:

length(permn(3))
dim(combn(3,2))[2]
无语# 2024-12-18 22:42:42

Combinations 包不是标准 CRAN 包集的一部分,而是不同存储库 omegahat 的一部分。要安装它,您需要使用

install.packages("Combinations", repos = "http://www.omegahat.org/R")

请参阅 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 use

install.packages("Combinations", repos = "http://www.omegahat.org/R")

See the documentation at http://www.omegahat.org/Combinations/

梦醒灬来后我 2024-12-18 22:42:42

可能是“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.

夜空下最亮的亮点 2024-12-18 22:42:42

上面的几种解决方案涉及旨在列出实际组合的函数,然后应用长度函数来执行计算。如果你只想要数字,这是非常低效的。在我的系统中使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文