title: performing a chi square test across multiple variables and extracting the relevant p value in R
body: <p>Ok straight to the question. I have a database with lots and lots of categorical variable.</p>
  
  <p>Sample database with a few variables as below</p>
  
  <pre><code>gender &lt;- as.factor(sample( letters[6:7], 100, replace=TRUE, prob=c(0.2, 0.8) ))    
  smoking &lt;- as.factor(sample(c(0,1),size=100,replace=T,prob=c(0.6,0.4)))    
  alcohol &lt;- as.factor(sample(c(0,1),size=100,replace=T,prob=c(0.3,0.7)))    
  htn &lt;- as.factor(sample(c(0,1),size=100,replace=T,prob=c(0.2,0.8)))    
  tertile &lt;- as.factor(sample(c(1,2,3),size=100,replace=T,prob=c(0.3,0.3,0.4)))    
  df &lt;- as.data.frame(cbind(gender,smoking,alcohol,htn,tertile))
  </code></pre>
  
  <p>I want to test the hypothesis, using a chi square test, that there is a difference in the portion of smokers, alcohol use, hypertension (htn) etc by tertile (3 factors). I then want to extract the p values for each variable.</p>
  
  <p>Now i know i can test each individual variable using a 2 by 3 cross tabulation but is there a more efficient code to derive the test statistic and p-value across all variables in one go and extract the p value across each variable</p>
  
  <p>Thanks in advance</p>
  
  <p>Anoop</p>