R找到所有可能的独特组合

时间:2018-03-29 19:18:27

标签: r combinations combn

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

我的问题是找到来自向量x的m个元素的组合,但是m可以大于x。例如,从字母[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"

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

如果之前有人问过同样的问题,请再次道歉,但我没有抓住它。感谢。

2 个答案:

答案 0 :(得分:2)

有一些专门为此构建的软件包。基本前提是我们需要重复长度为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

答案 1 :(得分: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"