【问题标题】:All possible combinations over groups组上所有可能的组合
【发布时间】:2015-01-12 09:51:13
【问题描述】:

我有 5 个组:G1、G2、…、G5,每组分别包含 n1、n2、…、n5 个元素。我从 4 组中的每组中选择 2 个元素,从第 5 组中选择 1 个元素。如何在 R 中生成所有可能的组合?

【问题讨论】:

  • 查看?combn 和?expand.grid。除此之外,尝试提供一个真实的数据集和期望的输出
  • 您的组是否互斥?即这些组可以与其他组具有相同的元素吗?请明确说明。如果他们是哑巴。除外。然后我们可以为组元素分配数字并尝试解决您的问题。

标签: r combinatorics


【解决方案1】:

(问题中没有说明组是否互斥;因此,假设:
1.组互斥
2. 组的子集 (n1, n2, ...) 将使用相同的元素填充)
3 只是为了论证 |G1|=|G2|= |G3|=5(用户可以根据组中不同数量的元素相应地更改以下代码) 以下是任何用户可以推广到任意数量组的问题的 3 组模拟答案。因此,假设组名是 G1、G2、G3。

library(causfinder)
gctemplate(5,2,2) # Elements are coded as: 1,2,3,4,5; |sub-G1|=2; |sub-G2|=2; |sub-G3|=5-(2+2)=1
# In the following table, each number represents a unique element. (SOLUTION ENDED!)

我的包裹 (causfinder) 不在 CRAN 中。因此,我将在下面给出函数gctemplate的代码。

      [,1] [,2] [,3] [,4] [,5]
 [1,]    1    2    3    4    5   sub-G1={1,2}  sub-G2={3,4} sub-G3={5}
 [2,]    1    2    3    5    4
 [3,]    1    2    4    5    3   sub-G1={1,2}  sub-G2={4,5} sub-G3={3}
 [4,]    1    3    2    4    5
 [5,]    1    3    2    5    4
 [6,]    1    3    4    5    2
 [7,]    1    4    2    3    5
 [8,]    1    4    2    5    3
 [9,]    1    4    3    5    2
[10,]    1    5    2    3    4
[11,]    1    5    2    4    3
[12,]    1    5    3    4    2
[13,]    2    3    1    4    5
[14,]    2    3    1    5    4
[15,]    2    3    4    5    1
[16,]    2    4    1    3    5
[17,]    2    4    1    5    3
[18,]    2    4    3    5    1
[19,]    2    5    1    3    4
[20,]    2    5    1    4    3
[21,]    2    5    3    4    1
[22,]    3    4    1    2    5
[23,]    3    4    1    5    2
[24,]    3    4    2    5    1
[25,]    3    5    1    2    4
[26,]    3    5    1    4    2
[27,]    3    5    2    4    1
[28,]    4    5    1    2    3
[29,]    4    5    1    3    2
[30,]    4    5    2    3    1

gctemplate的代码:

gctemplate <- function(nvars, ncausers, ndependents){
independents <- combn(nvars, ncausers)
patinajnumber <-  dim(combn(nvars - ncausers, ndependents))[[2]]
independentspatinajednumber <- dim(combn(nvars, ncausers))[[2]]*patinajnumber 
dependents <- matrix(, nrow = dim(combn(nvars, ncausers))[[2]]*patinajnumber, ncol = ndependents)
for (i in as.integer(1:dim(combn(nvars, ncausers))[[2]])){   
dependents[(patinajnumber*(i-1)+1):(patinajnumber*i),] <- t(combn(setdiff(seq(1:nvars), independents[,i]), ndependents))
}
independentspatinajed <- matrix(, nrow = dim(combn(nvars, ncausers))[[2]]*patinajnumber, ncol = ncausers)
for (i in as.integer(1:dim(combn(nvars, ncausers))[[2]])){   
for (j in as.integer(1:patinajnumber)){   
independentspatinajed[(i-1)*patinajnumber+j,] <- independents[,i]  
}}
independentsdependents <- cbind(independentspatinajed, dependents)
others <- matrix(, nrow = dim(combn(nvars, ncausers))[[2]]*patinajnumber, ncol = nvars - ncausers - ndependents) 
for (i in as.integer(1:((dim(combn(nvars, ncausers))[[2]])*patinajnumber))){  
others[i, ]  <- setdiff(seq(1:nvars), independentsdependents[i,]) 
}
causalitiestemplate <- cbind(independentsdependents, others)
causalitiestemplate
}

现在,G1,G2,G3 的解决方案如上。只需将上面的代码推广到具有相同逻辑的 5 变量情况即可!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2012-03-27
    • 2022-01-18
    相关资源
    最近更新 更多