I have the following data:
mydf[77:84,]
id game_week points code web_name first_name second_name position team_name date fixture team1 team2 home_away team_scored team_conceded minutes goals assists cleansheet goals_conceded own_goals
77 3 1 -2 51507 Koscielny Laurent Koscielny Defender Arsenal 17/08/13 ARS-AVL ARS AVL H 1 3 67 0 0 0 3 0
78 3 2 0 51507 Koscielny Laurent Koscielny Defender Arsenal 24/08/13 FUL-ARS ARS FUL A 3 1 0 0 0 0 0 0
79 3 3 6 51507 Koscielny Laurent Koscielny Defender Arsenal 01/09/13 ARS-TOT ARS TOT H 1 0 90 0 0 1 0 0
80 3 4 2 51507 Koscielny Laurent Koscielny Defender Arsenal 14/09/13 SUN-ARS ARS SUN A 3 1 90 0 0 0 1 0
81 3 5 2 51507 Koscielny Laurent Koscielny Defender Arsenal 22/09/13 ARS-STK ARS STK H 3 1 90 0 0 0 1 0
82 3 6 2 51507 Koscielny Laurent Koscielny Defender Arsenal 28/09/13 SWA-ARS ARS SWA A 2 1 90 0 0 0 1 0
83 3 7 3 51507 Koscielny Laurent Koscielny Defender Arsenal 06/10/13 WBA-ARS ARS WBA A 1 1 90 0 0 0 1 0
84 3 8 2 51507 Koscielny Laurent Koscielny Defender Arsenal 19/10/13 ARS-NOR ARS NOR H 4 1 90 0 0 0 1 0
As a part of modeling exercise, I want to create a new variable, "mov_avg_min", which for a given "id", is the average of "minutes" played in the last 3 "game_week". Example, for web_name "Koscielny" his distinct "id" is 3 in this data_frame. So for id= 3 and game_week=4, a function should calculate mov_avg_min of game_weeks 1:3 (3 game_week before current game_week for the same id value). Hence in row 80, mov_avg_min = 1/3(67+0+90)=52.333