0

I want to calculate the month value by calculating the mean of the weeksums per month.

e.g. for June (06) and distance 10 I have the weeksums 1(2017_28), 6(2017_29) and 1 (2017_31), I want to summarise these weeks to get the total monthsum 8 and the mean value 2.6667 (8:3).

I got the monthsum but I don't know how to calculate the mean

df %>% 
  group_by(year_month, distance) %>%
  mutate(monthsum = sum(weeksum))
   year year_month month year_week distance weeksum
1   2017    2017_05    05   2017_21       15       4
2   2017    2017_05    05   2017_21       10       1
3   2017    2017_05    05   2017_22        5       5
4   2017    2017_05    05   2017_22        0       1
5   2017    2017_06    06   2017_22        0      11
6   2017    2017_06    06   2017_23       20       7
7   2017    2017_06    06   2017_23        0       6
8   2017    2017_07    07   2017_28       10       1
9   2017    2017_07    07   2017_28        0       1
10  2017    2017_07    07   2017_29       10       6
11  2017    2017_07    07   2017_29        5       3
12  2017    2017_07    07   2017_30        0      12
13  2017    2017_07    07   2017_31       10       1
14  2017    2017_07    07   2017_31        0       7

This is what I want:

   year year_month month year_week distance monthsum    mean
1   2017    2017_05    05   2017_21       15       4      4
2   2017    2017_05    05   2017_21       10       1      1
3   2017    2017_05    05   2017_22        5       5      5
4   2017    2017_05    05   2017_22        0       1      1
5   2017    2017_06    06   2017_22        0      17    8.5
6   2017    2017_06    06   2017_23       20       7      7
7   2017    2017_07    07   2017_28       10       8 2.6667
8   2017    2017_07    07   2017_28        0      20 6.6667
9  2017    2017_07    07   2017_29        5       3      3


Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Isn't adding `monthmean = mean(weeksum))` to your `mutate` statement give what you want ? `df %>% group_by(year_month, distance) %>% mutate(monthsum = sum(weeksum), monthmean = mean(weeksum))` – Ronak Shah Nov 04 '19 at 11:06

1 Answers1

0

First of, I hope you use dplyrand not plyr to be up to date.

Also simply extend your statement with a mean() function like this:

df %>% 
   group_by(year_month, distance) %>% 
   mutate(monthsum = sum(weeksum), monthmean = mean(weeksum))

Also in your case use summarizeinstead of mutate to get a better view:

df %>% 
   group_by(year_month, distance) %>% 
   summarize(monthsum = sum(weeksum), monthmean = mean(weeksum))
Fnguyen
  • 1,159
  • 10
  • 23