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

  • 2
    Welcome to StackOverflow. Your post isn't [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) -- since your function is based on a specific dataset, can you provide your data? There are also some inconsistencies in your code -- are you trying to target the variable `Team` or `HomeTeam`? Is your dataset called `df` or `g`? – jrcalabrese Dec 03 '22 at 16:13

1 Answers1

0

Try mapply which is a multivariate version of sapply. If I understand your function correctly this should work:

df$Home_Last_3 = mapply(FUN=home_last3, df$HomeTeam, df$Game)
George Savva
  • 4,152
  • 1
  • 7
  • 21