0

looking to aggregate data (mean) in half-year periods by group.

Here is a snapshot of the data:

Date    Score   Group   Score2
01/01/2015  15  A   11
02/01/2015  34  A   33
03/01/2015  16  A   1
04/01/2015  29  A   36
05/01/2015  4   A   28
06/01/2015  10  B   33
07/01/2015  21  B   19
08/01/2015  6   B   47
09/01/2015  40  B   15
10/01/2015  34  B   13
11/01/2015  16  B   7
12/01/2015  8   B   4

I have dfd$mon<-as.yearmon(dfd$Date) then

r<-as.data.frame(dfd %>%
  mutate(month = format(Date, "%m"), year = format(Date, "%Y")) %>%
  group_by(Group,mon) %>%
  summarise(total = mean(Score), total1 = mean(Score2))) 

for monthly aggregation, but how would you do this for every 6 months, grouped by Group?

I sense I am overcomplicating a simple issue here!

Duke Showbiz
  • 252
  • 1
  • 11

1 Answers1

0

add another mutate after the current one:

mutate(yearhalf = as.integer(6/7)+1) %>%

output is 1 for the first 6 months and 2 for the months 7 to 12. Then you of course have to adapt the following functions for the new name, but that should do the trick.

Jan
  • 3,825
  • 3
  • 31
  • 51
  • Sorry Jan, I just get 1 for all rows? – Duke Showbiz Jun 26 '17 at 09:00
  • Can you provide code for the sample data as described [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), then I will provide the full code for your solution. did that first bit on the fly... – Jan Jun 26 '17 at 09:38