2x2x…の表

  • 総標本数Nでr1xr2x...というような任意の形の分割表を列挙したいとする
  • とりあえず興味があるのは2x2x...という限られたパターンとしよう
  • 分割表のセル数MについてNをM分割してM!の場合を考える、というのもありだが、2x2x...の場合には2分割を再帰的に実施してもよさそうだ
  • nの2分割は(0,n),(1,n-1),...,(n,0)のn+1通り、と単純なことがありがたい
divide.2 <- function(v,depth=1){
	tmp <- list()
	for(i in 1:length(v)){
		tmp[[i]] <- 0:v[i]
	}
	tmp2 <- expand.grid(tmp)
	tmp3 <- NULL
	for(i in 1:length(tmp2[1,])){
		tmp3 <- cbind(tmp3,tmp2[,i],v[i]-tmp2[,i])
	}
	if(depth==1){
		return(list(tmp3))
	}else{
		ret <- list()
		for(i in 1:length(tmp3[,1])){
			#print(tmp3[i,])
			ret[[i]] <- divide.2(tmp3[i,],depth=depth-1)
		}
		return(ret)
	}
	
}
divide.2(c(3),depth=4)
> divide.2(c(2),depth=3)
[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    0    0    0    0    0    0    2
[2,]    0    0    0    0    0    0    1    1
[3,]    0    0    0    0    0    0    2    0


[[1]][[2]]
[[1]][[2]][[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    0    0    0    0    1    0    1
[2,]    0    0    0    0    1    0    0    1
[3,]    0    0    0    0    0    1    1    0
[4,]    0    0    0    0    1    0    1    0


[[1]][[3]]
[[1]][[3]][[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    0    0    0    0    2    0    0
[2,]    0    0    0    0    1    1    0    0
[3,]    0    0    0    0    2    0    0    0



[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    0    0    1    0    0    0    1
[2,]    0    0    1    0    0    0    0    1
[3,]    0    0    0    1    0    0    1    0
[4,]    0    0    1    0    0    0    1    0


[[2]][[2]]
[[2]][[2]][[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    1    0    0    0    0    0    1
[2,]    1    0    0    0    0    0    0    1
[3,]    0    1    0    0    0    0    1    0
[4,]    1    0    0    0    0    0    1    0


[[2]][[3]]
[[2]][[3]][[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    0    0    1    0    1    0    0
[2,]    0    0    1    0    0    1    0    0
[3,]    0    0    0    1    1    0    0    0
[4,]    0    0    1    0    1    0    0    0


[[2]][[4]]
[[2]][[4]][[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    1    0    0    0    1    0    0
[2,]    1    0    0    0    0    1    0    0
[3,]    0    1    0    0    1    0    0    0
[4,]    1    0    0    0    1    0    0    0



[[3]]
[[3]][[1]]
[[3]][[1]][[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    0    0    2    0    0    0    0
[2,]    0    0    1    1    0    0    0    0
[3,]    0    0    2    0    0    0    0    0


[[3]][[2]]
[[3]][[2]][[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    1    0    1    0    0    0    0
[2,]    1    0    0    1    0    0    0    0
[3,]    0    1    1    0    0    0    0    0
[4,]    1    0    1    0    0    0    0    0


[[3]][[3]]
[[3]][[3]][[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    0    2    0    0    0    0    0    0
[2,]    1    1    0    0    0    0    0    0
[3,]    2    0    0    0    0    0    0    0