T=c()
N=1000
A = matrix
(c(30,20,10,20,15,15,10,10,8),3,3,byrow=TRUE)
T0=chisq.test(A)$statistic
x=c(rep(1,A[1,1]+A[1,2]+A[1,3]),rep(2,A[2,1]+A[2,2]
+A[2,3]),rep(3,A[3,1]+A[3,2]+A[3,3]))
y=c(rep(1,A[1,1]),rep(2,A[1,2]),rep(3,A[1,3]),
rep(1,A[2,1]),rep(2,A[2,2]),rep(3,A[2,3]),
rep(1,A[3,1]),rep(2,A[3,2]),rep(3,A[3,3]))
for (i in
1:N)
{ As=table(x,sample(y))
T=c(T,chisq.test(As)$statistic) }
sum(T>=T0)/N