title: Extracting group dependent results from a dataframe
body: <p>I have a dataframe made from different groups, and for each group real and predicted values. I want to extract values of tests on these values :</p>
  
  <pre><code>library(dplyr)
   d = data.frame(group = c(rep(5,x="a"),rep(5,x="b")), real = c(rep(2, x=1:5)), pred = c(2,1,3,4,5,1,2,4,3,5))
  
  group real pred
  1      a    1    2
  2      a    2    1
  3      a    3    3
  4      a    4    4
  5      a    5    5
  6      b    1    1
  7      b    2    2
  8      b    3    4
  9      b    4    3
  10     b    5    5
  
   d &lt;- d %&gt;% group_by(group) %&gt;% mutate( sg = ifelse(real == 1 &amp; real == pred, 1, 0))
   d &lt;- d %&gt;% group_by(group) %&gt;% mutate( sp = ifelse(real &lt;= 3 &amp; pred &lt;= 3, 1, 0))
   d %&gt;% distinct(sg, sp)
  
  sg    sp  group
  1     0     1      a
  2     0     0      a
  3     1     1      b
  4     0     1      b
  5     0     0      b
  </code></pre>
  
  <p>But I want something like this (only 1 result per group)</p>
  
  <pre><code> sg    sp  group
  1     0     1      a
  3     1     1      b
  </code></pre>
  
  <p>I am pretty sure dplyr, data.table or tidyr can do something but I cannot find how.</p>