减去超过 60 分钟的时间 [英] Subtracting times that exceed 60 minutes

查看:29
本文介绍了减去超过 60 分钟的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中包含足球赛事的时间安排.一场比赛超过 60 分钟,我正在尝试计算间隔.这是我拥有的数据:

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:

data <- c("11:14", "17:27", "25:34", "39:17", "39:59", "42:32", "50:15", "50:53", "64:22", "67:39")

我的问题是由于分钟数超过 60(可能介于 0 到 90 分钟之间)而引起的.所以基本上,我想要的代码可以打印出事件之间的间隔:

My issue arises from the fact that minutes exceed 60 (they can be between 0 and 90 minutes). So essentially, I would like code that would print out the intervals between the events:

"6:13", "8:07", "13:43",..., "3:17"

将数据转换为小时然后从那里开始会更好吗?只是一个想法,我必须让它更容易.我查看了其他问题,但我找不到任何要求 R 的问题.如果有但我错过了,请随时批评,但请将副本链接给我.

Would it be better to convert the data into hours and then go from there? Just an idea I had to make it easier. I've had a look at other questions, but I couldn't find any that had been asked for R. If it has and I missed it, feel free to criticize but please link me the duplicate.

提前致谢!

推荐答案

针对这种事情考虑 lubridate.

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

如果您希望输出为格式化字符串而不是句点,请使用 sprintf:

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

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

这篇关于减去超过 60 分钟的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆