I am trying to calculate the average number of goals home and away team has scored, but "todays" game.
The data can be found here : http://www.football-data.co.uk/mmz4281/1415/E0.csv
My code
pl <- pl[,2:6]
pl$Date <- as.Date(pl$Date, "%d/%m/%y")
pl$HomeTeam <- as.character(pl$HomeTeam)
pl$AwayTeam <- as.character(pl$AwayTeam)
pl.func <- function(tf){
tf$avg.ht <- rep(NA,nrow(tf))
tf$avg.at <- rep(NA,nrow(tf))
for(i in 1:nrow(tf)){
tf$avg.ht[i] <- (sum(tf$FTHG[tf$HomeTeam == tf$HomeTeam[i] & tf$Date < tf$Date[i]]) + sum(tf$FTAG[tf$AwayTeam == tf$HomeTeam[i] & tf$Date <tf$Date[i]])) / sum(tf$HomeTeam == tf$HomeTeam[i] & tf$Date < tf$Date[i] | tf$AwayTeam == tf$HomeTeam[i] & tf$Date < tf$Date[i])
tf$avg.at[i] <- (sum(tf$FTHG[tf$HomeTeam == tf$AwayTeam[i] & tf$Date < tf$Date[i]]) + sum(tf$FTAG[tf$AwayTeam == tf$AwayTeam[i] & tf$Date <tf$Date[i]])) / sum(tf$HomeTeam == tf$AwayTeam[i] & tf$Date < tf$Date[i] | tf$AwayTeam == tf$AwayTeam[i] & tf$Date < tf$Date[i])
}
return(tf)
}
pl <- pl.func(pl)
I need to "match" on team, and a earlier date. The above code works, but is slow as I want to calculate several hundreds of calculation. Can anyone hint or show how I can do this with some kind of apply function? I could not succed as I dont know to to replace the [i] argument from the loop on a correct way.