I have a football dataset for a season and some variable are: player_id
, week
and points
(a grade for each player in a match).
So, each player_id
appears several times in my dataset.
My goal is to calculate the average points for each player, but just to previous weeks.
For example, to the row where player_id=5445
and week=10
, I want the mean when data has player_id=5445
and week is from 1 to 9.
I know I can do it filtering data for each row and calculating it. But I hope to do it in a smarter/faster way...
I thought something like:
aggregate(mydata$points, FUN=mean,
by=list(player_id=mydata$player_id, week<mydata$week))
but it did not work
Thankss!!!