title: Chi -Square test with grouped data in dplyr
body: <p>I have difficulties summarising a <code>data.frame</code> that looks like this:</p>
  
  <pre><code>db &lt;- data.frame(ID = c(rep(1, 3), rep(2,4), rep(3, 2), 4),
               Gender = factor(c(rep("woman", 7), rep("man", 2), "woman")),
               Grade = c(rep(3, 3), rep(1, 4), rep(2, 2), 1),
               Drug = c(1, 2, 2, 1, 2, 6, 9, 8, 5, 1),
               Group = c(rep(1, 3), rep(2,4), rep(1, 2), 2))
  db
  
  #    ID Gender Grade Drug Group
  # 1   1  woman     3    1     1
  # 2   1  woman     3    2     1
  # 3   1  woman     3    2     1
  # 4   2  woman     1    1     2
  # 5   2  woman     1    2     2
  # 6   2  woman     1    6     2
  # 7   2  woman     1    9     2
  # 8   3    man     2    8     1
  # 9   3    man     2    5     1
  # 10  4  woman     1    1     2
  </code></pre>
  
  <p>Ideally, I would have one row per observation, but because the <code>Drugs</code> vary over time I end-up with a lot of duplicate rows. This makes the analysis difficult for me.</p>
  
  <p>My ultimate goal is to construct a summary table as already discussed in another post: <a href="https://stackoverflow.com/questions/34587317/using-dplyr-to-create-summary-proportion-table-with-several-categorical-factor-v">Using dplyr to create summary proportion table with several categorical/factor variables</a>. Something like this:</p>
  
  <p>| Variable | Group 1 | Group 2 | difference Group 1/2 |<br>
  | <strong>Gender</strong>  ................................| .........................p = 1 |<br>
  | Male.....     |...........1 | ............0 | ..................................|<br>
  | Female.   |...........1 |.............2 |...................................|</p>
  
  <p>However, since this post was only partially answered and is not directly applicable to my problem (mainly due to the duplicate rows), I would already be happy if could perform the summary statistics separately. In this post: <a href="https://stackoverflow.com/questions/49283009/how-to-get-the-frequency-from-grouped-data-with-dplyr">How to get the frequency from grouped data with dplyr?</a> I asked how to obtain the unique/distinct frequencies from the observations. Now, I need to find out if there is a <em>statistical significant difference in the distribution of the genders between the two groups</em>.</p>
  
  <hr>
  
  <p>According to the <code>ID</code>, I know that there are four observations of which three are female and one is male. So the desired outcome could be calculated like this:</p>
  
  <pre><code>gen &lt;- factor(c("woman", "woman", "man", "woman"))
  gr &lt;- c(1, 2 ,1 ,2)
  chisq.test(gen, gr)
  
  #   Pearson's Chi-squared test with Yates' continuity correction
  # 
  # data:  gen and gr
  # X-squared = 0, df = 1, p-value = 1
  #
  # Warning message:
  # In chisq.test(gen, gr) : Chi-squared approximation may be incorrect
  </code></pre>
  
  <p><strong>How can I calculate the p-vale from my <code>data.frame</code> with the use of <code>dplyr</code>?</strong></p>
  
  <hr>
  
  <p>My failing approach was:</p>
  
  <pre><code>db %&gt;% 
    group_by(ID) %&gt;% 
    distinct(ID, Gender, Group) %&gt;% 
    summarise_all(funs(chisq.test(db$Gender, 
                                 db$Group)$p.value))
  # A tibble: 4 x 3
  #      ID Gender Group
  #  &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;
  # 1    1.  0.429 0.429
  # 2    2.  0.429 0.429
  # 3    3.  0.429 0.429
  # 4    4.  0.429 0.429
  # Warning messages:
  # 1: In chisq.test(db$Gender, db$Group) :
  #   Chi-squared approximation may be incorrect
  # 2: In chisq.test(db$Gender, db$Group) :
  #   Chi-squared approximation may be incorrect
  # 3: In chisq.test(db$Gender, db$Group) :
  #  Chi-squared approximation may be incorrect
  # 4: In chisq.test(db$Gender, db$Group) :
  #  Chi-squared approximation may be incorrect
  # 5: In chisq.test(db$Gender, db$Group) :
  #   Chi-squared approximation may be incorrect
  # 6: In chisq.test(db$Gender, db$Group) :
  #  Chi-squared approximation may be incorrect
  # 7: In chisq.test(db$Gender, db$Group) :
  #  Chi-squared approximation may be incorrect
  # 8: In chisq.test(db$Gender, db$Group) :
  #  Chi-squared approximation may be incorrect
  </code></pre>