在R中创建结果组,每个元素仅使用一次(不重复的组合)。

huangapple go评论63阅读模式
英文:

Creating result groups in R, using each element once (combination without repetition)

问题

我有一个包含6个个体的数据集:A,B,C,D,E,F。

我想将它们分成两组,每组包含三个个体,并已经在R中使用combn函数完成了这个任务:

m <- combn(n, 3)

这为我提供了所有20种可能的组合,其中个体出现在多个组中。然后,我想找到所有可能的结果组合,其中每个个体只能使用一次。

我想要使用不重复的组合来完成这个任务:
C(n,r) = n! / r!(n-r)!,因此我会得到10个结果,看起来像这样:

  • abc + def
  • abd + cef
  • abe + cdf
  • abf + cde
  • acd + bef
  • ace + bdf
  • acf + bde
  • ade + bcf
  • adf + bce
  • aef + bcd

我不太确定如何在R中编写这个代码,从我生成的组合列表中获取这些结果。

编辑:要生成我正在使用的数据集,我使用了以下代码:

individuals <- c("a","b","c","d","e","f")
n <- length(individuals)
x <- 3
comb = function(n, x) {
  factorial(n) / factorial(n-x) / factorial(x)
}
comb(n,x)
(m <- combn(n, 3)) 
numbers <- m
letters <- individuals
for (i in 1:length(numbers)) {
  m[i] <- letters[numbers[i]]
}
英文:

I have a dataset of 6 individuals: A,B,C,D,E,F

I want to group these into two groups of three individuals and have done so with the combn function in R:

m &lt;- combn(n, 3)

This gives me all 20 possible groups where individuals occur in multiple groups. From this set of groups I then went to find all possible combinations of results, where each individual can only be used once.

I would like to do this using combinations without repetition:
C(n,r) = n! / r!(n-r)! and would therefore get 10 results that would look like this:

  • abc + def
  • abd + cef
  • abe + cdf
  • abf + cde
  • acd + bef
  • ace + bdf
  • acf + bde
  • ade + bcf
  • adf + bce
  • aef + bcd

I am not sure how to code this in R, from the list of groups that I have generated.

Edit: to generate the dataset I am using I have used the following code:

individuals &lt;- c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;)
n &lt;- length(individuals)
x &lt;- 3
comb = function(n, x) {
  factorial(n) / factorial(n-x) / factorial(x)
}
comb(n,x)
(m &lt;- combn(n, 3)) 
numbers &lt;- m
letters &lt;- individuals
for (i in 1:length(numbers)) {
  m[i] &lt;- letters[numbers[i]]
}

答案1

得分: 1

以下是您要翻译的代码部分的内容:

In base R:

  1. 创建 3 个字母的组合,并将其存储在列表中(asplit
  2. 创建 2 个组(每个组包含 3 个字母)的新组合
  3. 使用 Filter 仅保留两部分没有共同元素的组合
individuals &lt;- c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;)
combn(individuals, 3, simplify = FALSE) |&gt;
  combn(m = 2, simplify = FALSE) |&gt;
  Filter(f = \(x) !any(x[[1]] %in% x[[2]]))

输出:

[[1]]
[[1]][[1]]
[1] &quot;a&quot; &quot;b&quot; &quot;c&quot;

[[1]][[2]]
[1] &quot;d&quot; &quot;e&quot; &quot;f&quot;


[[2]]
[[2]][[1]]
[1] &quot;a&quot; &quot;b&quot; &quot;d&quot;

[[2]][[2]]
[1] &quot;c&quot; &quot;e&quot; &quot;f&quot;


[[3]]
[[3]][[1]]
[1] &quot;a&quot; &quot;b&quot; &quot;e&quot;

[[3]][[2]]
[1] &quot;c&quot; &quot;d&quot; &quot;f&quot;


[[4]]
[[4]][[1]]
[1] &quot;a&quot; &quot;b&quot; &quot;f&quot;

[[4]][[2]]
[1] &quot;c&quot; &quot;d&quot; &quot;e&quot;


[[5]]
[[5]][[1]]
[1] &quot;a&quot; &quot;c&quot; &quot;d&quot;

[[5]][[2]]
[1] &quot;b&quot; &quot;e&quot; &quot;f&quot;


[[6]]
[[6]][[1]]
[1] &quot;a&quot; &quot;c&quot; &quot;e&quot;

[[6]][[2]]
[1] &quot;b&quot; &quot;d&quot; &quot;f&quot;


[[7]]
[[7]][[1]]
[1] &quot;a&quot; &quot;c&quot; &quot;f&quot;

[[7]][[2]]
[1] &quot;b&quot; &quot;d&quot; &quot;e&quot;


[[8]]
[[8]][[1]]
[1] &quot;a&quot; &quot;d&quot; &quot;e&quot;

[[8]][[2]]
[1] &quot;b&quot; &quot;c&quot; &quot;f&quot;


[[9]]
[[9]][[1]]
[1] &quot;a&quot; &quot;d&quot; &quot;f&quot;

[[9]][[2]]
[1] &quot;b&quot; &quot;c&quot; &quot;e&quot;


[[10]]
[[10]][[1]]
[1] &quot;a&quot; &quot;e&quot; &quot;f&quot;

[[10]][[2]]
[1] &quot;b&quot; &quot;c&quot; &quot;d&quot;
英文:

In base R:

  1. Create combnations of 3 letters and store it in a list (asplit)
  2. Create new combnations of 2 groups (of 3 letters)
  3. Filter the list to only keep combinations where the both parts have no element in common
individuals &lt;- c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;)
combn(individuals, 3, simplify = FALSE) |&gt;
  combn(m = 2, simplify = FALSE) |&gt;
  Filter(f = \(x) !any(x[[1]] %in% x[[2]]))

output

[[1]]
[[1]][[1]]
[1] &quot;a&quot; &quot;b&quot; &quot;c&quot;

[[1]][[2]]
[1] &quot;d&quot; &quot;e&quot; &quot;f&quot;


[[2]]
[[2]][[1]]
[1] &quot;a&quot; &quot;b&quot; &quot;d&quot;

[[2]][[2]]
[1] &quot;c&quot; &quot;e&quot; &quot;f&quot;


[[3]]
[[3]][[1]]
[1] &quot;a&quot; &quot;b&quot; &quot;e&quot;

[[3]][[2]]
[1] &quot;c&quot; &quot;d&quot; &quot;f&quot;


[[4]]
[[4]][[1]]
[1] &quot;a&quot; &quot;b&quot; &quot;f&quot;

[[4]][[2]]
[1] &quot;c&quot; &quot;d&quot; &quot;e&quot;


[[5]]
[[5]][[1]]
[1] &quot;a&quot; &quot;c&quot; &quot;d&quot;

[[5]][[2]]
[1] &quot;b&quot; &quot;e&quot; &quot;f&quot;


[[6]]
[[6]][[1]]
[1] &quot;a&quot; &quot;c&quot; &quot;e&quot;

[[6]][[2]]
[1] &quot;b&quot; &quot;d&quot; &quot;f&quot;


[[7]]
[[7]][[1]]
[1] &quot;a&quot; &quot;c&quot; &quot;f&quot;

[[7]][[2]]
[1] &quot;b&quot; &quot;d&quot; &quot;e&quot;


[[8]]
[[8]][[1]]
[1] &quot;a&quot; &quot;d&quot; &quot;e&quot;

[[8]][[2]]
[1] &quot;b&quot; &quot;c&quot; &quot;f&quot;


[[9]]
[[9]][[1]]
[1] &quot;a&quot; &quot;d&quot; &quot;f&quot;

[[9]][[2]]
[1] &quot;b&quot; &quot;c&quot; &quot;e&quot;


[[10]]
[[10]][[1]]
[1] &quot;a&quot; &quot;e&quot; &quot;f&quot;

[[10]][[2]]
[1] &quot;b&quot; &quot;c&quot; &quot;d&quot;

huangapple
  • 本文由 发表于 2023年2月8日 17:38:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定