home_last3 = function(team,game){
g1 = filter(g, Team == team & Game > game)
g1 = head(g1, n=3)
return(sum(g1$goals))
}
print(home_last3("Arsenal","5"))
[1] 11
So I have this function above. The idea is that in my dataset of all Premier League matches, I want to know how many goals the home team scored in their previous 3 matches. That dataframe g has all the games and goals scored for each team listed.
So my function does exactly what I want it to when I plug stuff in manually.
However, I want to apply this function to a new column (Home_Last_3) in my original dataset, where for any given match it can look at the name of the home team (col = "HomeTeam") and the number of the game (col="Game") and spit out that number.
However, when I inputed this:
df$Home_Last_3 = home_last3(df$HomeTeam,df$Game)
It just spit back zeros in all columns.
I think it has something to do with using as.data.frame, or sapply (but sapply got mad at me for having that second argument in there).