0

I am trying to get the average performance for the all following rows in each group. The grouping factor is ID and the variable to be averaged is performance. The data is already ordered by the variable day.

Desired output example: For ID A on Day 5 the mean would be 8.25[(9+6+10+8)/4]. For ID A on Day 6 the mean would be 8[(6+10+8)/3]. So basically the mean of the remaining performance scores ordered by the day variable.

I am looking to do this in R and currently have failed as the roll_mean function creates NA's as it meets the end of each group.

Any help much appreciated, thanks!

structure(list(ID = c("A", "A", "A", "A", "A", "A", "A", "A", 
                  "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "C", "C", 
                  "C", "C", "C", "C", "D", "D", "D", "D", "D", "D", "D", "D"), 
           Day = c(1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
                   10, 11, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8), 
           Performance = c(5, 
                           3, 3, 6, 9, 6, 10, 8, 10, 9, 2, 5, 8, 4, 6, 3, 6, 3, 2, 4, 
                           4, 9, 10, 10, 3, 4, 2, 9, 4, 7, 4, 9, 6)), 
      .Names = c("ID", 
                 "Day", "Performance"), class = c("tbl_df", "tbl", "data.frame"
                 ), 
      row.names = c(NA, -33L))
George
  • 674
  • 2
  • 7
  • 19
  • What is the expected output? - Why do you need a rolling mean? – Sotos Jun 18 '17 at 15:30
  • This is a duplicate of: https://stackoverflow.com/questions/21982987/mean-per-group-in-a-data-frame – CPak Jun 18 '17 at 15:30
  • Please clarify what do you mean by "the mean of remaining rows"? If you are talking about a moving average, what would the the size of the moving window? – www Jun 18 '17 at 15:34
  • For example: for **ID A** on **Day 5** the mean would be 8.25[(9+6+10+8)/4].For **ID A** on **Day 6** the mean would be 8[(6+10+8)/3]. So basically the mean of the remaining performance scores ordered by the day variable. – George Jun 19 '17 at 06:49

0 Answers0