I have a data frame that I want to group by two variables, and then summarize the total and average.
I tried this on my data, which is correct.
df %>%
  group_by(date, group) %>%
  summarise(
    weight = sum(ind_weigh) ,
    total_usage = sum(total_usage_min) ,
    Avg_usage = total_usage / weight) %>% 
  ungroup()
It returns this data frame:
df <- tibble::tribble(
     ~date, ~group,   ~weight, ~total_usage, ~Avg_usage,
  20190201,      0,  450762,     67184943,        149,
  20190201,      1, 2788303,    385115718,        138,
  20190202,      0,  483959,     60677765,        125,
  20190202,      1, 2413699,    311226351,        129,
  20190203,      0,  471189,     59921762,        127,
  20190203,      1, 2143811,    277425186,        129,
  20190204,      0,  531020,     83695977,        158,
  20190204,      1, 2640087,    403200829,        153
  )
I am wondering how can I add another variable in my script to get the avg_usage_total(for both group 0 and group 1) as well.
Expected result:
ex, first row --> (67184943 / (450762 + 2788303) = 20.7
date    group   rech    total_usage Avg_usage   Avg_usage_total
20190201    0   450762  67184943    149             20.7
20190201    1   2788303 385115718   138             118.9
 
    