Subtracting times that exceed 60 minutes

后端 未结 3 913
忘了有多久
忘了有多久 2021-01-24 01:41

I have a dataset which contains the timing of events in football. A game exceeds 60 minutes, and I\'m trying to calculate intervals. This is the data I have:

dat         


        
3条回答
  •  余生分开走
    2021-01-24 02:10

    Another base R attempt using as.difftime to specify the units explicitly:

    out <- diff(sapply(strsplit(data, ":"), function(x) 
      Reduce(`+`, Map(as.difftime, as.numeric(x),  units=c("mins","secs"))))
    )
    
    # time difference in seconds
    out
    #[1] 373 487 823  42 153 463  38 809 197
    
    # formatted string
    sprintf("%d:%02d", out %/% 60, out %% 60)
    #[1] "6:13"  "8:07"  "13:43" "0:42"  "2:33"  "7:43"  "0:38"  "13:29" "3:17"
    

提交回复
热议问题