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))