不确定我是否选择了一个好的标题...而且我不知道我是否使用了正确的术语,所以也许使用正确的搜索字词我会找到解决这个问题的方法...
我有一个字符串列表,我希望从中获得 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 个字符串)。在每个集合中,每个字符串不得出现超过一次。
mylist
当然,如果这对于不是 的倍数的长度也是可能的,那就太好了n=3
。如果我们假设有 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"
...
有人对此有解决方案吗?如果我的解释不清楚,请告诉我。
干杯