函数combn()一次生成m个x元素的所有组合。对于nCm小(其中n是x的元素数),这是非常快速且高效的,但很快就会耗尽内存。例如:

> combn(c(1:50), 12, simplify = TRUE)
Error in matrix(r, nrow = len.r, ncol = count) :
invalid 'ncol' value (too large or NA)

我想知道是否可以修改功能combn()使其仅生成k个选定的组合。我们将此新函数称为selectedcombn()。然后我们将有:
> combn(c("a", "b", "c", "d"), m=2)
     [,1] [,2] [,3] [,4] [,5] [,6]
 [1,] "a"  "a"  "a"  "b"  "b"  "c"
 [2,] "b"  "c"  "d"  "c"  "d"  "d"

>chosencombn(c("a", "b", "c", "d"), m=2, i=c(1,4,6))
     [,1] [,2] [,3]
 [1,] "a"  "b"  "c"
 [2,] "b"  "c"  "d"

>chosencombn(c("a", "b", "c", "d"), m=2, i=c(4,5))
     [,1] [,2]
 [1,] "b"  "b"
 [2,] "c"  "d"

我知道这样的功能将需要使用组合的排序,以便人们可以立即找到给定组合的位置。
是否存在这样的顺序?可以对其进行编码以获得与combn()一样高效的功能吗?

最佳答案

软件包"trotter"对此很有用,因为它不会将排列保留在内存中。

library(trotter)

combs = cpv(2, c("a", "b", "c", "d"))
sapply(c(1, 4, 6), function(i) combs[i])
#     [,1] [,2] [,3]
#[1,] "a"  "b"  "c"
#[2,] "b"  "c"  "d"

09-25 19:58