[英]Number of combinations with restrictions in R
Dear R and stats community,亲爱的 R 和统计社区,
How many combinations are possible to select 3 objects from a total of 9 objects in a way that 3 selected objects do not repeat themselves in one external characteristic?从总共 9 个对象中选择 3 个对象,以使 3 个所选对象不会在一个外部特征中重复自己,有多少种组合可能?
In my example, three selected specimens must always differ in their species attribute.在我的示例中,三个选定的样本在物种属性上必须始终不同。 That is, in the selection of 3, there should always be one representative of species A, one of species B and one of species C, and the order within this selection does not matter.
也就是说,在选择3中,应该总是有一个代表物种A,一个代表物种B,一个代表物种C,并且这个选择中的顺序无关紧要。
> mydata <- data.frame(specimens = paste("s", 1:9, sep = ""), species = LETTERS[1:3])
> mydata
specimens species
1 s1 A
2 s2 B
3 s3 C
4 s4 A
5 s5 B
6 s6 C
7 s7 A
8 s8 B
9 s9 C
How many combinations are there?有多少组合? I know how to count the combinations of ANY set of three objects with eg
arrangements::ncombinations(9,3)
or choose(9,3)
.我知道如何计算任何一组三个对象的组合,例如
arrangements::ncombinations(9,3)
或choose(9,3)
。
I have seen that one way for generating combinations that allows pasting a custom function which could help in selecting combinations with required properties.我已经看到了一种生成组合的方法,它允许粘贴自定义函数,这有助于选择具有所需属性的组合。
library(utils)
combn(mydata$specimens, 3, FUN)
I am not able to design such a function by myself.我无法自己设计这样的功能。 One of the unsuccessful trials is
失败的试验之一是
library(utils)
outp <- combn(as.character(mydata$specimens), 3, function(x) !duplicated(mydata$species))
> outp[, 1:10]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[2,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[3,] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[5,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[6,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[7,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[8,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[9,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Thanks in advance for your help.在此先感谢您的帮助。
There has to be an A, a B and a C. There are 3 possible As.必须有 A、B 和 C。有 3 种可能的 As。 Each of these must appear with one of the Bs, so there are 3 * 3 = 9 possible pairs of As and Bs.
这些中的每一个都必须与一个 B 一起出现,因此有 3 * 3 = 9 对可能的 As 和 B。 Each of these pairs can be grouped with one of the 3 Cs, so there are 3 * 3 * 3 = 27 possible combinations.
这些对中的每一对都可以与 3 个 C 中的一个组合在一起,因此有 3 * 3 * 3 = 27 种可能的组合。
You can see them all using this one-liner:您可以使用此单线查看它们:
expand.grid(split(mydata$specimens, mydata$species))
#> A B C
#> 1 s1 s2 s3
#> 2 s4 s2 s3
#> 3 s7 s2 s3
#> 4 s1 s5 s3
#> 5 s4 s5 s3
#> 6 s7 s5 s3
#> 7 s1 s8 s3
#> 8 s4 s8 s3
#> 9 s7 s8 s3
#> 10 s1 s2 s6
#> 11 s4 s2 s6
#> 12 s7 s2 s6
#> 13 s1 s5 s6
#> 14 s4 s5 s6
#> 15 s7 s5 s6
#> 16 s1 s8 s6
#> 17 s4 s8 s6
#> 18 s7 s8 s6
#> 19 s1 s2 s9
#> 20 s4 s2 s9
#> 21 s7 s2 s9
#> 22 s1 s5 s9
#> 23 s4 s5 s9
#> 24 s7 s5 s9
#> 25 s1 s8 s9
#> 26 s4 s8 s9
#> 27 s7 s8 s9
You can try interaction
over split
like below您可以尝试通过
split
interaction
,如下所示
> levels(interaction(with(mydata, split(specimens,species))))
[1] "s1.s2.s3" "s4.s2.s3" "s7.s2.s3" "s1.s5.s3" "s4.s5.s3" "s7.s5.s3"
[7] "s1.s8.s3" "s4.s8.s3" "s7.s8.s3" "s1.s2.s6" "s4.s2.s6" "s7.s2.s6"
[13] "s1.s5.s6" "s4.s5.s6" "s7.s5.s6" "s1.s8.s6" "s4.s8.s6" "s7.s8.s6"
[19] "s1.s2.s9" "s4.s2.s9" "s7.s2.s9" "s1.s5.s9" "s4.s5.s9" "s7.s5.s9"
[25] "s1.s8.s9" "s4.s8.s9" "s7.s8.s9"
which shows you all combinations of component specimens
from different species
它向您展示了来自不同
species
的组件specimens
所有组合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.