title: Chi Square Analysis using for loop in R
body: <p>I'm trying to do chi square analysis for all combinations of variables in the data and my code is:</p>
  
  <pre><code>Data &lt;- esoph[ , 1:3]
  OldStatistic &lt;- NA
  for(i in 1:(ncol(Data)-1)){
  for(j in (i+1):ncol(Data)){
  Statistic &lt;- data.frame("Row"=colnames(Data)[i], "Column"=colnames(Data)[j],
                       "Chi.Square"=round(chisq.test(Data[ ,i], Data[ ,j])$statistic, 3),
                       "df"=chisq.test(Data[ ,i], Data[ ,j])$parameter,
                       "p.value"=round(chisq.test(Data[ ,i], Data[ ,j])$p.value, 3),
                        row.names=NULL)
  temp &lt;- rbind(OldStatistic, Statistic)
  OldStatistic &lt;- Statistic
  Statistic &lt;- temp
  }
  }
  
  str(Data)
  'data.frame':   88 obs. of  3 variables:
   $ agegp: Ord.factor w/ 6 levels "25-34"&lt;"35-44"&lt;..: 1 1 1 1 1 1 1 1 1 1 ...
   $ alcgp: Ord.factor w/ 4 levels "0-39g/day"&lt;"40-79"&lt;..: 1 1 1 1 2 2 2 2 3 3 ...
   $ tobgp: Ord.factor w/ 4 levels "0-9g/day"&lt;"10-19"&lt;..: 1 2 3 4 1 2 3 4 1 2 ...
  
  
  Statistic
      Row Column Chi.Square df p.value
  1 agegp  tobgp      2.400 15       1
  2 alcgp  tobgp      0.619  9       1
  </code></pre>
  
  <p>My code gives my the chi square analysis output for variable 1 vs variable 3, and variable 2 vs variable 3 and is missing for variable 1 vs variable 2. I tried hard but could not fixed the code. Any comment and suggestion will be highly appreciated. I'd like like to do cross tabulation for all possible combinations. Thanks in advance.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I used to do this kind of analysis in SPSS but now I want to switch to R.</p>