将小时和分钟转换为分钟

huangapple go评论67阅读模式
英文:

Convert hours and minutes into minutes

问题

I have a vector of event times in the form of hours, minutes, and seconds. Now I wish to convert all of these times into minutes. For example, 01:02:18 would become 62 minutes.

My data:

times
"01:02:18" "01:47:37" "01:18:48" "01:32:54" "01:09:41" "01:50:30"

Code I tried but it did not work:

time = format(strptime(matchtime,"%H:%M:%S"),'%M')

Guessing there is something that could be done by adding the hours and minutes. However, I am unsure on how to do that as well.

英文:

I have a vector of event times in the form of hours, minutes, and seconds. Now I wish to convert all of these times into minutes. For example, 01:02:18 would become 62 minutes.

My data:

times
"01:02:18" "01:47:37" "01:18:48" "01:32:54" "01:09:41" "01:50:30"

Code I tried but it did not work:

time = format(strptime(matchtime,"%H:%M:%S"),'%M')

Guessing there is something that could be done by adding the hours and minutes. However, I am unsure on how to do that as well.

答案1

得分: 4

如果我们想提取分钟和小时

library(lubridate)
v1 <- hms(times)
v1@hour * 60 + v1@minute
[1] 62 107 78 92 69 110


或者包括秒

period_to_seconds(hms(times))/60
[1] 62.30000 107.61667 78.80000 92.90000 69.68333 110.50000

英文:

If we want to extract the minutes and hours

library(lubridate)
v1 &lt;- hms(times)
v1@hour * 60 + v1@minute
[1]  62 107  78  92  69 110

Or to include the seconds as well

period_to_seconds(hms(times))/60
[1]  62.30000 107.61667  78.80000  92.90000  69.68333 110.50000

答案2

得分: 3

在基本的R中:

difftime(strptime(matchtime, "%T"), strptime('0:0:0', '%T'), units = 'min')
时间差异(以分钟为单位)
[1]  62.30000 107.61667  78.80000  92.90000  69.68333 110.50000

同样的效果:

as.numeric(strptime(matchtime, "%T") - strptime('0:0:0', "%T"), 'mins')
[1]  62.30000 107.61667  78.80000  92.90000  69.68333 110.50000

编辑

使用data.table:

as.numeric(data.table::as.ITime(matchtime))/60
[1]  62.30000 107.61667  78.80000  92.90000  69.68333 110.50000
英文:

in base R:

difftime(strptime(matchtime,&quot;%T&quot;), strptime(&#39;0:0:0&#39;,&#39;%T&#39;), units = &#39;min&#39;)
Time differences in mins
[1]  62.30000 107.61667  78.80000  92.90000  69.68333 110.50000

same as:

as.numeric(strptime(matchtime,&quot;%T&quot;) - strptime(&#39;0:0:0&#39;, &quot;%T&quot;),&#39;mins&#39;)
[1]  62.30000 107.61667  78.80000  92.90000  69.68333 110.50000

Edit

using data.table:

as.numeric(data.table::as.ITime(matchtime))/60
[1]  62.30000 107.61667  78.80000  92.90000  69.68333 110.50000

答案3

得分: 2

你可以使用 as.difftime 并将单位设置为 mins

as.difftime(times, units = "mins")
#以分钟为单位的时间差
#[1]  62.30000 107.61667  78.80000  92.90000  69.68333 110.50000

要忽略秒数,请使用 floor

floor(as.difftime(times, units = "mins"))
#以分钟为单位的时间差
#[1]  62 107  78  92  69 110

数据

times <- c("01:02:18", "01:47:37", "01:18:48", "01:32:54", "01:09:41", "01:50:30")
英文:

You can use as.difftime and set the units to mins.

as.difftime(times, units = &quot;mins&quot;)
#Time differences in mins
#[1]  62.30000 107.61667  78.80000  92.90000  69.68333 110.50000

To ignore the seconds use floor.

floor(as.difftime(times, units = &quot;mins&quot;))
#Time differences in mins
#[1]  62 107  78  92  69 110

Data

times &lt;- c(&quot;01:02:18&quot;, &quot;01:47:37&quot;, &quot;01:18:48&quot;, &quot;01:32:54&quot;, &quot;01:09:41&quot;, &quot;01:50:30&quot;)

huangapple
  • 本文由 发表于 2023年3月31日 23:20:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定