title: How to do a RMSE-calculation for every subgroup of dataframe using a dplyr pipe
body: <p>This is my_data:</p>
  <pre><code>p0  | country_ID | WinFlag | 
  ----------------------------
  0.17| UK         | 0       |
  0.85| GER        | 1       |
  0.55| USA        | 0       |
  0.25| USA        | 0       |
  0.80| UK         | 1       |
  </code></pre>
  <p>etc.</p>
  <p>I do succeed to caculate the RMSE from my_data with the code below:</p>
  <pre><code>test &lt;- my_data %&gt;%
        group_by(predic = round(p0, digits = 2)) %&gt;%
        summarize(count = n(), wf = mean(WinFlag)) %&gt;%
        mutate(vwap_err_sq = ((predic-wf)^2)*count)
  
        rmse_p0 &lt;- sqrt(sum(test$vwap_err_sq) / sum(test$count))*100
  
   &gt;   test:
            predic  count  wf    vwap_err_sq
              &lt;dbl&gt; &lt;int&gt;  &lt;dbl&gt;       &lt;dbl&gt;
           1   0.04     6 0          0.0096 
           2   0.05     8 0.125      0.045  
           3   0.06    19 0.158      0.182  
           4   0.07    20 0.2        0.338  
  </code></pre>
  <p>Next I try to calculate a specific RMSE for every country_ID in my_data.
  Is it possible to do this with the pipe from dplyr?
  I have tried to add group_by(country_ID) within the code above in several ways although I do not succeed to generate a specific RMSE for every country_ID.</p>
  <p>Thanks a lot!</p>
  <p>Below I have added a reproducible dataset with the solution:</p>
  <pre><code>library(tidyverse)
  
  my_data &lt;- data.frame(p0 = c(0.17,0.17,0.85,0.55,0.25,0.80),
                        country_ID=c(&quot;UK&quot;,&quot;UK&quot;,&quot;GER&quot;,&quot;USA&quot;,&quot;USA&quot;,&quot;UK&quot;),
                        WinFlag=c(1,0,1,0,0,1))
  
  
  test &lt;- my_data %&gt;%
    group_by(predic = round(p0, digits = 2)) %&gt;%
    mutate(count = 1) %&gt;%
    mutate(wf = mean(WinFlag)) %&gt;%
    mutate(vwap_err_sq = ((predic-wf)^2)*1) %&gt;%
    ungroup() %&gt;%
    group_by(country_ID) %&gt;%
    mutate(rmse_p0 = sqrt(sum(vwap_err_sq*count) / sum(count))*100)
  </code></pre>