0

So I have a df like this:

Data

structure(list(ID = 1:12, Team = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L), DOW = c("Monday", "Monday", "Tuesday", "Tuesday", 
"Wednesday", "Wednesday", "Thursday", "Thursday", "Monday", "Tuesday", 
"Tuesday", "Monday"), Hrs = c(11858L, 1338L, 13282L, 2188L, 15831L, 
858L, 34858L, 10000L, 1000L, 23320L, 1000L, 23000L)), class = "data.frame", row.names = c(NA, 
-12L))
 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

Let's call each row an incident.

So I want to calculate the rate of incidents for each day of the week. I want to do this based on Team as well.

Using the following I can generate the average

df %>% 
  group_by(Team, DOW) %>% 
  summarize(count = n(),
            hrs   = sum(Hrs)) %>% 
  mutate(incRate = count/hrs) %>% 

How exactly would I make this a weighted average. I need two different weighted averages tho.

  1. Weighted average taking into account both teams
  2. Weighted average which only takes into account the appropriate team.

So for 2, it would be a weighted average for just Team 1 data for their avg and Team 2 for theirs..

In short, I want to add 2 variables which would be weighted averages.

Lastly I would like to plot using the following:

ggplot(df, aes(y=incRate, x=as.factor(Day.of.Week), color=FRA.Reportable,
                        group=FRA.Reportable)) +
  geom_line() +
  labs(x = "Day of the Week", y = "IncRate", fill = "") +
  scale_color_manual(values = c("darkorange", "cornflowerblue") ) +
  theme_hc() +
  theme(axis.text.x=element_text(angle = 45))
Tho Vu
  • 1,304
  • 2
  • 8
  • 20
John Thomas
  • 1,075
  • 9
  • 32
  • 1
    Is this what you want: `df %>% group_by(Team, DOW) %>% summarize(count = n(), hrs = sum(Hrs)) %>% mutate(incRate = count/hrs,wm = weighted.mean(hrs,incRate))` ? – Duck Aug 14 '20 at 20:44
  • 1
    Also `df %>% group_by(Team, DOW) %>% mutate(N=n(),hrs = sum(Hrs),incRate = N/hrs,wm = weighted.mean(hrs,incRate)) `? – Duck Aug 14 '20 at 20:49

0 Answers0