title: Get `chisq.test()$p.value` for several groups using `dplyr::group_by()`
body: <p>I'm trying to conduct a chi square test on several groups <em>within the dplyr frame</em>. The problem is,  <code>group_by() %&gt;% summarise()</code> doesn't seem to do trick.</p>
  
  <p>Simulated data (same structure as problematic data, but random, so p.values should be high)</p>
  
  <pre><code>set.seed(1)
  data.frame(partido=sample(c("PRI", "PAN"), 100, 0.6),
         genero=sample(c("H", "M"), 100, 0.7), 
         GM=sample(c("Bajo", "Muy bajo"), 100, 0.8)) -&gt; foo
  </code></pre>
  
  <p>I want to compare several groups defined by GM to see if there are changes in the p.values for the crosstab of partido and genero, conditional to GM. </p>
  
  <p>The obvious dplyr way should be: </p>
  
  <pre><code>foo %&gt;% 
    group_by(GM) %&gt;% 
    summarise(pvalue=chisq.test(.$partido, .$genero)$p.value)  #just the p.value, so summarise is happy
  </code></pre>
  
  <p>But I get the p.values for the ungrouped data, just to times, not the p.value for each table:    </p>
  
  <p><code># A tibble: 2 × 2
  GM         pvalue
  &lt;fctr&gt;     &lt;dbl&gt;
  1     Bajo 0.8660521
  2 Muy bajo 0.8660521</code></p>
  
  <p>Testing each group using filter I get: </p>
  
  <pre><code>foo %&gt;% 
    filter(GM=="Bajo") %$% 
    table(partido, genero) %&gt;% 
    chisq.test()
  </code></pre>
  
  <p>Returns: <code>X-squared = 0.015655, df = 1, p-value = 0.9004</code></p>
  
  <pre><code>foo %&gt;% 
    filter(GM=="Muy bajo") %$% 
    table(partido, genero) %&gt;% chisq.test()
  </code></pre>
  
  <p>Returns: <code>X-squared = 0.50409, df = 1, p-value = 0.4777</code></p>
  
  <p><code>dplyr:summarise()</code> works with functions with more than one argument, so this shouldn't be the problem: </p>
  
  <pre><code>data.frame(a=1:10, b=10:1, c=sample(c("Grupo 1", "Grupo 2"), 10, 0.5)) %&gt;% 
      group_by(c) %&gt;% 
      summarise(r=cor(a, b))
  </code></pre>
  
  <p>works like charm. It just doesn't seem to work with chisq.test. </p>
  
  <p>I managed to get what I wanted with nested models using <code>tidyr::nest()</code> and <code>purrr::map()</code>, but I find the code cumbersome --at least for my students. Actually, I´ve invested many ours teaching them (a very math and programming challenged group) dplyr so they could avoid vector functions as much as possible.   </p>
  
  <pre><code>foo %&gt;% 
    nest(-GM) %&gt;% 
    mutate(tabla=map(data, ~table(.))) %&gt;% 
    mutate(pvalue=map(tabla, ~chisq.test(.)$p.value)) %&gt;% 
    select(GM, pvalue) %&gt;% 
    unnest()
  
  A tibble: 2 × 2
         GM   pvalue
      &lt;fctr&gt;  &lt;dbl&gt;
  1     Bajo  0.9004276
  2 Muy bajo  0.4777095
  </code></pre>
  
  <p><code>do()</code> does the trick too:</p>
  
  <pre><code>foo %&gt;% 
    group_by(GM) %&gt;% 
    do(tidy(chisq.test(.$partido, .$genero)))
  
  Source: local data frame [2 x 5]
  Groups: GM [2]
      GM statistic   p.value parameter
  &lt;fctr&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;int&gt;
  1     Bajo 0.0156553 0.9004276         1
  2 Muy bajo 0.5040878 0.4777095         1
  # ... with 1 more variables: method &lt;fctr&gt;
  </code></pre>
  
  <p>as in: <a href="https://stackoverflow.com/questions/32841453/fishers-and-pearsons-test-for-indepedence/32844251">Fisher&#39;s and Pearson&#39;s test for indepedence</a></p>
  
  <p>But, ¿why doesn't <code>group_by()</code> work with <code>summarise(chisq.test()$p.value)</code>? </p>