R 中的简单组合数学

发布于 2024-12-14 10:10:23 字数 363 浏览 1 评论 0原文

我希望分享一个 R 函数,用于查找单个向量的元素之间所有可能的唯一无向组合:

combi <- function(vec1)
{
  si <- length(vec1)
  first <- rep(vec1, (si-1):0)
  secR <- rev(vec1)
  second <- secR[sequence(1:(si-1))]
  second <- rev(second)
  combi <- matrix(cbind(first, second), ncol = 2)
  return(combi)
}

并询问是否有更简单的方法来做到这一点? (我需要结果位于两列矩阵中)。

I wish to share an R function for finding all possible unique undirected combinations between elements of a single vector:

combi <- function(vec1)
{
  si <- length(vec1)
  first <- rep(vec1, (si-1):0)
  secR <- rev(vec1)
  second <- secR[sequence(1:(si-1))]
  second <- rev(second)
  combi <- matrix(cbind(first, second), ncol = 2)
  return(combi)
}

and ask if there is a simpler way of doing this? (I need the result to be in a 2-column matrix).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

小耗子 2024-12-21 10:10:23

嗯,有一个内置的 combn 函数:

t(combn(vec1,2))

不过,你的看起来更快,也许是因为 combn 正在尝试解决一个更普遍的问题(??):

> library(rbenchmark)
> v <- 1:20
> benchmark(combi(v),t(combn(v,2)))
            test replications elapsed relative user.self sys.self
1       combi(v)          100   0.005      1.0     0.004    0.000   
2 t(combn(v, 2))          100   0.044      8.8     0.040    0.004   

Well, there's a built-in combn function:

t(combn(vec1,2))

Yours looks faster, though, perhaps because combn is trying to solve a more general problem (??):

> library(rbenchmark)
> v <- 1:20
> benchmark(combi(v),t(combn(v,2)))
            test replications elapsed relative user.self sys.self
1       combi(v)          100   0.005      1.0     0.004    0.000   
2 t(combn(v, 2))          100   0.044      8.8     0.040    0.004   
毁我热情 2024-12-21 10:10:23

utils 中有一个基本的 R 函数 combn,据我所知,它给出了相同的(如果转置)结果。不同之处在于,combn 更灵活,因为它还可以计算除 2 之外的长度组合。

combi(1:5)
      [,1] [,2]
 [1,]    1    2
 [2,]    1    3
 [3,]    1    4
 [4,]    1    5
 [5,]    2    3
 [6,]    2    4
 [7,]    2    5
 [8,]    3    4
 [9,]    3    5
[10,]    4    5

使用基本 R combn

combn(1:5, 2)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    1    1    1    2    2    2    3    3     4
[2,]    2    3    4    5    3    4    5    4    5     5

计算长度为 3 的组合:

combn(1:5, 3)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    1    1    1    1    1    2    2    2     3
[2,]    2    2    2    3    3    4    3    3    4     4
[3,]    3    4    5    4    5    5    4    5    5     5

There is a base R function combn in package utils which, as far as I can tell, gives identical (if transposed) results. The difference is that combn is more flexible in the sense that it will also calculate combinations of length other than 2.

combi(1:5)
      [,1] [,2]
 [1,]    1    2
 [2,]    1    3
 [3,]    1    4
 [4,]    1    5
 [5,]    2    3
 [6,]    2    4
 [7,]    2    5
 [8,]    3    4
 [9,]    3    5
[10,]    4    5

Using the base R combn:

combn(1:5, 2)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    1    1    1    2    2    2    3    3     4
[2,]    2    3    4    5    3    4    5    4    5     5

Calculate combinations of length 3:

combn(1:5, 3)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    1    1    1    1    1    2    2    2     3
[2,]    2    2    2    3    3    4    3    3    4     4
[3,]    3    4    5    4    5    5    4    5    5     5
困倦 2024-12-21 10:10:23

combinat。用于组合、排列等的大量工具。

package combinat. Great bunch of tools for combination, permutation, and all that.

无需解释 2024-12-21 10:10:23

感谢您发帖。对性能进行了一些调整。

一个。在首先计算索引时,我使用了rep.int 或rep。

b.我用

second <- secR[rev(sequence(1:(si-1)))]

代替了

second <- secR[sequence(1:(si-1))]
second <- rev(second)

c 。我用

matrix(c(first, second), ncol = 2)

而不是

matrix(cbind(first, second), ncol = 2)

Thanks for posting. A few tweaks for performance.

a. I used rep.int instead or rep, when figuring out the indices for first.

b. I used

second <- secR[rev(sequence(1:(si-1)))]

instead of

second <- secR[sequence(1:(si-1))]
second <- rev(second)

c. I used

matrix(c(first, second), ncol = 2)

instead of

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