Subtracting times that exceed 60 minutes

后端 未结 3 912
忘了有多久
忘了有多久 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 01:52

    Look into lubridate for this kind of thing.

    There's probably an easier way to do it, but this works:

    library(lubridate)
    data <- c("11:14", "17:27", "25:34", "39:17", "39:59", "42:32", "50:15", "50:53", "64:22", "67:39")
    out <- seconds_to_period(diff(as.numeric(ms(data)))
    

    If you want the output as a formatted string instead of a period, use sprintf:

    sprintf('%02d:%02d', minute(out), second(out))
    

提交回复
热议问题