r - R 中组合作为元素的梳状任务

标签 r combinations combinatorics combn

不确定我是否选择了一个好的标题...而且我也不知道我是否使用了正确的术语,所以也许使用正确的搜索词我会找到这个问题的解决方案...

我有一个字符串列表,我想从中获得 3 的所有“独占”组合集。

示例: 具有以下内容

require(utils)
mylist<-c("strA","strB","strC","strD","strE","strF")
t(combn(mylist,3))

我得到一个表格,列出了这 6 个字符串中 3 个字符串的所有可能组合(因此每一行代表 3 个字符串的一个组合):

        [,1]   [,2]   [,3]  
   [1,] "strA" "strB" "strC"
   [2,] "strA" "strB" "strD"
   [3,] "strA" "strB" "strE"
   [4,] "strA" "strB" "strF"
   [5,] "strA" "strC" "strD"
   [6,] "strA" "strC" "strE"
   [7,] "strA" "strC" "strF"
   [8,] "strA" "strD" "strE"
   [9,] "strA" "strD" "strF"
  [10,] "strA" "strE" "strF"
  [11,] "strB" "strC" "strD"
  [12,] "strB" "strC" "strE"
  [13,] "strB" "strC" "strF"
  [14,] "strB" "strD" "strE"
  [15,] "strB" "strD" "strF"
  [16,] "strB" "strE" "strF"
  [17,] "strC" "strD" "strE"
  [18,] "strC" "strD" "strF"
  [19,] "strC" "strE" "strF"
  [20,] "strD" "strE" "strF"

但我想要 3 组的所有组合,其中每个字符串只出现一次。所以我想要的输出看起来像这样:

$1
        [,1]   [,2]   [,3]  
   [1,] "strA" "strB" "strC"
   [2,] "strD" "srtE" "strF"
$2
        [,1]   [,2]   [,3]  
   [1,] "strA" "strB" "strD"
   [1,] "strC" "strE" "strF"
$3
        [,1]   [,2]   [,3]  
   [1,] "strA" "strB" "strE"
   [1,] "strC" "strD" "strF"
...

所以这里每个子元素($1$2$3等)包含3个字符串的2个组合(如2*3= 6;有 6 根弦)。在每个集合中,每个字符串不得出现多次。

当然,如果这对于不是 n=3 倍数的 mylist 长度也是可能的,那就太好了。如果我们有 10 个字符串(加上“strG”、“strH”、“strI”和“strJ”),我希望在每个组合中省略一个字符串。所以想要的结果会是这样的

$1
        [,1]   [,2]   [,3]  
   [1,] "strA" "strB" "strC"
   [2,] "strD" "strE" "strF"
   [3,] "strG" "strH" "strI"
$2
        [,1]   [,2]   [,3]  
   [1,] "strA" "strB" "strC"
   [2,] "strD" "strE" "strF"
   [3,] "strG" "strH" "strJ"
$3
        [,1]   [,2]   [,3]  
   [1,] "strA" "strB" "strC"
   [2,] "strD" "strE" "strF"
   [3,] "strG" "strI" "strJ"
$4
        [,1]   [,2]   [,3]  
   [1,] "strA" "strB" "strC"
   [2,] "strE" "strF" "strG"
   [3,] "strH" "strI" "strJ"
...

有人有办法解决这个问题吗? 如果我的解释不清楚,请告诉我。

干杯

最佳答案

假设转置组合矩阵名为mat。检查应用于 intersect 函数结果的长度是否有重叠:

 res <- list();
 for (i in 1:nrow(mat) ){
    for( j in 1:nrow(mat)){  
          if( !length(intersect(mat[i,] , mat[j,])) ) 
               res[[paste(i,j,sep="_")]] <- rbind( mat[i,], mat[j, ]) } }


> res
$`1_20`
     [,1]   [,2]   [,3]  
[1,] "strA" "strB" "strC"
[2,] "strD" "strE" "strF"

$`2_19`
     [,1]   [,2]   [,3]  
[1,] "strA" "strB" "strD"
[2,] "strC" "strE" "strF"

$`3_18`
     [,1]   [,2]   [,3]  
[1,] "strA" "strB" "strE"
[2,] "strC" "strD" "strF"

.... snipped

根据您对“唯一”的定义,您可能决定只采用前十项,因为其中一半是行的转置:

> res[[1]]
     [,1]   [,2]   [,3]  
[1,] "strA" "strB" "strC"
[2,] "strD" "strE" "strF"
> res[[20]]
     [,1]   [,2]   [,3]  
[1,] "strD" "strE" "strF"
[2,] "strA" "strB" "strC"

关于r - R 中组合作为元素的梳状任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34072257/

相关文章:

java - 加载 xlsx 包时出错

R:RGraphviz 安装

r - 在 R 中生成卡方分布的随机数

python - (python) 我如何以特定方式对组合的所有结果求和?

algorithm - 以较低的成本将元素分配给多个目标的最佳方法是什么?

R:找到所有值的 95% 范围的函数?

r - 如何将计数器分配给由因子组合定义的 data.frame 的特定子集?

javascript - 在 Javascript 中列出顺序子集元素

algorithm - 如何创建没有两首连续歌曲在同一调中的轨道列表

生成循环对列表的算法