title: Weighted averages in R grouped by variable using dplyr
body: <p>So I have a df like this:</p>
  <p><strong>Data</strong></p>
  <pre><code>structure(list(ID = 1:12, Team = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
  2L, 1L, 2L, 1L, 2L), DOW = c(&quot;Monday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, &quot;Tuesday&quot;, 
  &quot;Wednesday&quot;, &quot;Wednesday&quot;, &quot;Thursday&quot;, &quot;Thursday&quot;, &quot;Monday&quot;, &quot;Tuesday&quot;, 
  &quot;Tuesday&quot;, &quot;Monday&quot;), Hrs = c(11858L, 1338L, 13282L, 2188L, 15831L, 
  858L, 34858L, 10000L, 1000L, 23320L, 1000L, 23000L)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -12L))
  </code></pre>
  <pre><code> ID  Team         DOW    Hrs
  001     1      Monday  11858
  002     2      Monday   1338
  003     1     Tuesday  13282
  004     2     Tuesday   2188 
  005     1   Wednesday  15831
  006     2   Wednesday    858
  007     1    Thursday  34858
  008     2    Thursday  10000
  009     1      Monday   1000
  010     2     Tuesday  23320
  011     1     Tuesday   1000
  012     2      Monday  23000
  </code></pre>
  <p>Let's call each row an incident.</p>
  <p>So I want to calculate the rate of incidents for each day of the week. I want to do this based on <code>Team</code> as well.</p>
  <p>Using the following I can generate the average</p>
  <pre><code>df %&gt;% 
    group_by(Team, DOW) %&gt;% 
    summarize(count = n(),
              hrs   = sum(Hrs)) %&gt;% 
    mutate(incRate = count/hrs) %&gt;% 
  </code></pre>
  <p>How exactly would I make this a weighted average. I need two different weighted averages tho.</p>
  <ol>
  <li>Weighted average taking into account both teams</li>
  <li>Weighted average which only takes into account the appropriate team.</li>
  </ol>
  <p>So for 2, it would be a weighted average for just Team 1 data for their avg and Team 2 for theirs..</p>
  <p>In short, I want to add 2 variables which would be weighted averages.</p>
  <p>Lastly I would like to plot using the following:</p>
  <pre><code>ggplot(df, aes(y=incRate, x=as.factor(Day.of.Week), color=FRA.Reportable,
                          group=FRA.Reportable)) +
    geom_line() +
    labs(x = &quot;Day of the Week&quot;, y = &quot;IncRate&quot;, fill = &quot;&quot;) +
    scale_color_manual(values = c(&quot;darkorange&quot;, &quot;cornflowerblue&quot;) ) +
    theme_hc() +
    theme(axis.text.x=element_text(angle = 45))
  </code></pre>