5

我试图在 R 中找到所有可能的独特组合。似乎已经提出了很多类似的问题,但我找不到相同的问题。

我的问题是从向量 x 中找到 m 个元素的组合,但 m 可能大于 x。例如,从 letters[1:2] 中选择 3 个元素,希望可以返回:

combn(letters[1:2],3)
       [,1]  [,2]  [,3]  [,4] 
[1,]   "a"   "a"   "a"    "b"
[2,]   "a"   "a"   "b"    "b"
[3,]   "a"   "b"   "b"    "b"

但是combn函数n < m中的错误。有类似的功能,包括 gtools:permutations、expand.grid。

如果之前有人问过同样的问题,请再次道歉,但我没有听懂。谢谢。

4

2 回答 2

6

有一些专门为此构建的软件包。基本前提是我们需要长度重复的组合,m其中m可能大于输入向量。我们从经典开始gtools

library(gtools)
combinations(2, 3, letters[1:2], repeats.allowed = TRUE)
    [,1] [,2] [,3]
[1,] "a"  "a"  "a" 
[2,] "a"  "a"  "b" 
[3,] "a"  "b"  "b" 
[4,] "b"  "b"  "b"

然后有arrangements一个替代品iterpc(上面评论中@Gregor链接的包):

library(arrangements)
arrangements::combinations(2, 3, letters[1:2], replace = TRUE)
     [,1] [,2] [,3]
[1,] "a"  "a"  "a" 
[2,] "a"  "a"  "b" 
[3,] "a"  "b"  "b" 
[4,] "b"  "b"  "b"

最后是RcppAlgos我撰写的:

library(RcppAlgos)
comboGeneral(letters[1:2], 3, TRUE)
     [,1] [,2] [,3]
[1,] "a"  "a"  "a" 
[2,] "a"  "a"  "b" 
[3,] "a"  "b"  "b" 
[4,] "b"  "b"  "b"

combn是一个很棒的功能,它作为基本包之一附带R,但是它的一个缺点是它不允许重复(这是这里需要的)。我为与此类似的问题写了一个非常全面的概述,可以在这里找到:A Walk Through a Slice of Combinatorics in R

于 2018-03-30T01:53:10.550 回答
1
combn1=function(x,m){
  n=ifelse(length(x)==1,ifelse(is.numeric(x),x,1),length(x))
  if(n>=m) return(combn(x,m))
  a=do.call(expand.grid, rep(list(x),m))
  b=t(unique(t(apply(a,1,sort))))
  `dimnames<-`(b,NULL)
}

combn1(letters[1],3)
     [,1]
[1,] "a" 
[2,] "a" 
[3,] "a" 
> combn1(letters[1:2],3)
     [,1] [,2] [,3] [,4]
[1,] "a"  "a"  "a"  "b" 
[2,] "a"  "a"  "b"  "b" 
[3,] "a"  "b"  "b"  "b" 
> combn1(letters[1:3],3)
     [,1]
[1,] "a" 
[2,] "b" 
[3,] "c" 
于 2018-03-29T20:22:34.540 回答