title: Select group with highest mean in R
body: <p>I've been dealing for a while with this problem but still haven't found the optimal solution. Hope you can help me.</p>
  <p>I have a dataset with several columns, that I need to filter. As an example let's say that I have the following:</p>
  <pre><code>DB &lt;- data.frame(A = c(rep(&quot;GeneA&quot;,6), rep(&quot;GeneB&quot;,6)), B = c(&quot;one&quot;, &quot;one&quot;, &quot;two&quot;, &quot;two&quot;, &quot;three&quot;, &quot;three&quot;, &quot;one&quot;, &quot;one&quot;, &quot;two&quot;, &quot;two&quot;, &quot;three&quot;, &quot;three&quot;), C = c(1,2,5,4,8,5,8,7,4,5,1,8))
  </code></pre>
  <p>What I would like to have is a filtered dataset for each value in column A with the groups in column B with highest mean.</p>
  <p>In that case the desired output would be:</p>
  <pre><code>DB &lt;- data.frame(A = c(&quot;GeneA&quot;,&quot;GeneA&quot;,&quot;GeneB&quot;,&quot;GeneB&quot;), B = c(&quot;three&quot;, &quot;three&quot;, &quot;one&quot;, &quot;one&quot;), C = c(8,5,8,7))
  </code></pre>
  <p>Searching on the web only have found the case where you can filter the highest value of each group and I need each row of the group.</p>
  <p>With:</p>
  <pre><code>result &lt;- DB %&gt;%
    group_by(A,B) %&gt;%
    summarize(c = mean(C))
  </code></pre>
  <p>I just obtain the means. I have also tried with aggegate, etc. but no way. And i'm sure that there is a easy way, probably with data.table.</p>