2015-01-12 166 views
0

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

+4

看看'?combn'和'?expand.grid'。除此之外,请尝试提供*真实*数据集和所需的输出 –

+0

您的小组是否相互排斥?即组可以与其他组具有相同的元素吗?请明确说明。如果他们是mut。 EXC。那么我们可以将数字分配给组元素并尝试解决您的问题。 –

回答

0

(它不是在问题中指定的组是否是相互排斥的与否;所以,假设:
1.基团是相互排斥的
2.子集组(N1,N2的,...)将在填充时使用相同的元素)
仅用于参数| 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变量情况!