英文:
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 <- 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,"%T"), strptime('0:0:0','%T'), units = 'min')
Time differences in mins
[1] 62.30000 107.61667 78.80000 92.90000 69.68333 110.50000
same as:
as.numeric(strptime(matchtime,"%T") - strptime('0:0:0', "%T"),'mins')
[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 = "mins")
#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 = "mins"))
#Time differences in mins
#[1] 62 107 78 92 69 110
Data
times <- c("01:02:18", "01:47:37", "01:18:48", "01:32:54", "01:09:41", "01:50:30")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论