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

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

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中编写这个代码,从我生成的组合列表中获取这些结果。

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

  1. individuals <- c("a","b","c","d","e","f")
  2. n <- length(individuals)
  3. x <- 3
  4. comb = function(n, x) {
  5. factorial(n) / factorial(n-x) / factorial(x)
  6. }
  7. comb(n,x)
  8. (m <- combn(n, 3))
  9. numbers <- m
  10. letters <- individuals
  11. for (i in 1:length(numbers)) {
  12. m[i] <- letters[numbers[i]]
  13. }
英文:

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:

  1. individuals &lt;- c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;)
  2. n &lt;- length(individuals)
  3. x &lt;- 3
  4. comb = function(n, x) {
  5. factorial(n) / factorial(n-x) / factorial(x)
  6. }
  7. comb(n,x)
  8. (m &lt;- combn(n, 3))
  9. numbers &lt;- m
  10. letters &lt;- individuals
  11. for (i in 1:length(numbers)) {
  12. m[i] &lt;- letters[numbers[i]]
  13. }

答案1

得分: 1

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

In base R:

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

输出:

  1. [[1]]
  2. [[1]][[1]]
  3. [1] &quot;a&quot; &quot;b&quot; &quot;c&quot;
  4. [[1]][[2]]
  5. [1] &quot;d&quot; &quot;e&quot; &quot;f&quot;
  6. [[2]]
  7. [[2]][[1]]
  8. [1] &quot;a&quot; &quot;b&quot; &quot;d&quot;
  9. [[2]][[2]]
  10. [1] &quot;c&quot; &quot;e&quot; &quot;f&quot;
  11. [[3]]
  12. [[3]][[1]]
  13. [1] &quot;a&quot; &quot;b&quot; &quot;e&quot;
  14. [[3]][[2]]
  15. [1] &quot;c&quot; &quot;d&quot; &quot;f&quot;
  16. [[4]]
  17. [[4]][[1]]
  18. [1] &quot;a&quot; &quot;b&quot; &quot;f&quot;
  19. [[4]][[2]]
  20. [1] &quot;c&quot; &quot;d&quot; &quot;e&quot;
  21. [[5]]
  22. [[5]][[1]]
  23. [1] &quot;a&quot; &quot;c&quot; &quot;d&quot;
  24. [[5]][[2]]
  25. [1] &quot;b&quot; &quot;e&quot; &quot;f&quot;
  26. [[6]]
  27. [[6]][[1]]
  28. [1] &quot;a&quot; &quot;c&quot; &quot;e&quot;
  29. [[6]][[2]]
  30. [1] &quot;b&quot; &quot;d&quot; &quot;f&quot;
  31. [[7]]
  32. [[7]][[1]]
  33. [1] &quot;a&quot; &quot;c&quot; &quot;f&quot;
  34. [[7]][[2]]
  35. [1] &quot;b&quot; &quot;d&quot; &quot;e&quot;
  36. [[8]]
  37. [[8]][[1]]
  38. [1] &quot;a&quot; &quot;d&quot; &quot;e&quot;
  39. [[8]][[2]]
  40. [1] &quot;b&quot; &quot;c&quot; &quot;f&quot;
  41. [[9]]
  42. [[9]][[1]]
  43. [1] &quot;a&quot; &quot;d&quot; &quot;f&quot;
  44. [[9]][[2]]
  45. [1] &quot;b&quot; &quot;c&quot; &quot;e&quot;
  46. [[10]]
  47. [[10]][[1]]
  48. [1] &quot;a&quot; &quot;e&quot; &quot;f&quot;
  49. [[10]][[2]]
  50. [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
  1. individuals &lt;- c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;)
  2. combn(individuals, 3, simplify = FALSE) |&gt;
  3. combn(m = 2, simplify = FALSE) |&gt;
  4. Filter(f = \(x) !any(x[[1]] %in% x[[2]]))

output

  1. [[1]]
  2. [[1]][[1]]
  3. [1] &quot;a&quot; &quot;b&quot; &quot;c&quot;
  4. [[1]][[2]]
  5. [1] &quot;d&quot; &quot;e&quot; &quot;f&quot;
  6. [[2]]
  7. [[2]][[1]]
  8. [1] &quot;a&quot; &quot;b&quot; &quot;d&quot;
  9. [[2]][[2]]
  10. [1] &quot;c&quot; &quot;e&quot; &quot;f&quot;
  11. [[3]]
  12. [[3]][[1]]
  13. [1] &quot;a&quot; &quot;b&quot; &quot;e&quot;
  14. [[3]][[2]]
  15. [1] &quot;c&quot; &quot;d&quot; &quot;f&quot;
  16. [[4]]
  17. [[4]][[1]]
  18. [1] &quot;a&quot; &quot;b&quot; &quot;f&quot;
  19. [[4]][[2]]
  20. [1] &quot;c&quot; &quot;d&quot; &quot;e&quot;
  21. [[5]]
  22. [[5]][[1]]
  23. [1] &quot;a&quot; &quot;c&quot; &quot;d&quot;
  24. [[5]][[2]]
  25. [1] &quot;b&quot; &quot;e&quot; &quot;f&quot;
  26. [[6]]
  27. [[6]][[1]]
  28. [1] &quot;a&quot; &quot;c&quot; &quot;e&quot;
  29. [[6]][[2]]
  30. [1] &quot;b&quot; &quot;d&quot; &quot;f&quot;
  31. [[7]]
  32. [[7]][[1]]
  33. [1] &quot;a&quot; &quot;c&quot; &quot;f&quot;
  34. [[7]][[2]]
  35. [1] &quot;b&quot; &quot;d&quot; &quot;e&quot;
  36. [[8]]
  37. [[8]][[1]]
  38. [1] &quot;a&quot; &quot;d&quot; &quot;e&quot;
  39. [[8]][[2]]
  40. [1] &quot;b&quot; &quot;c&quot; &quot;f&quot;
  41. [[9]]
  42. [[9]][[1]]
  43. [1] &quot;a&quot; &quot;d&quot; &quot;f&quot;
  44. [[9]][[2]]
  45. [1] &quot;b&quot; &quot;c&quot; &quot;e&quot;
  46. [[10]]
  47. [[10]][[1]]
  48. [1] &quot;a&quot; &quot;e&quot; &quot;f&quot;
  49. [[10]][[2]]
  50. [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:

确定