How to use R to calculate time breaks in goals scored in football

I would like to get a numerical vector of time intervals between goals scored by a football team

df <- data.frame(game=c(1,2,3,4,5,6,6,6,7),goaltime=c(NA,35,51,NA,NA,2,81,90,15))

NA indicates that the team did not score a goal in this game. The earliest goal you can score in the game 1

Each game has a total time of 90 minutes, so the output vector should be

c(125,106,221,79,9,15,75)
+4
source share
1 answer

You may try:

diff(c(0,setdiff(90*(df$game-1)+df$goaltime,NA),90*max(df$game)))
#[1] 125 106 221  79   9  15  75
+4
source

Source: https://habr.com/ru/post/1620568/


All Articles