How to convert decimal time to time format [duplicate]

这一生的挚爱 提交于 2021-02-08 06:56:23

问题


I would like to calculate the difference between two time-sets. I can do this, but I only get the difference in decimals and I would like to know how to convert them to format as in "Minutes:Second".

So, I have the minutes and the seconds as characters:

video_begin <- c("8:14", "4:47", "8:27", "4:59", "4:57", "7:51", "6:11", "5:30")
video_end <- c("39:08", "47:10", "49:51", "44:31", "39:41", "47:12", "40:13", "46:52")

I convert them into time values with as.POSIXct, make a df and add the difference as a third column, easy peasy...

video_begin <- as.POSIXct(video_begin, format = "%M:%S")
video_end <- as.POSIXct(video_end, format = "%M:%S")
video <- data.frame(video_begin, video_end)
video$video_duration <- video_end - video_begin

And this is what I get for video:

  video_begin         video_end            video_duration
1 2017-09-12 00:08:14 2017-09-12 00:39:08  30.90000 mins
2 2017-09-12 00:04:47 2017-09-12 00:47:10  42.38333 mins
3 2017-09-12 00:08:27 2017-09-12 00:49:51  41.40000 mins
4 2017-09-12 00:04:59 2017-09-12 00:44:31  39.53333 mins
5 2017-09-12 00:04:57 2017-09-12 00:39:41  34.73333 mins
6 2017-09-12 00:07:51 2017-09-12 00:47:12  39.35000 mins
7 2017-09-12 00:06:11 2017-09-12 00:40:13  34.03333 mins
8 2017-09-12 00:05:30 2017-09-12 00:46:52  41.36667 mins

How do I change the format of video$video_duration from decimal to the same format as in video$video_begin and video$video_end: "Minutes:Seconds" (I don't care about day, month, year and hour)?

I tried:

video$video_duration <- as.POSIXct(video$video_duration, format = "%M:%S")

and

strptime(video$video_duration, format="%M:%S")

but nah...

I found some answers but I'm not very satisfied with them:

How convert decimal to POSIX time

Algorithm to convert Text time to Decimal Time

Isn't there a more... handy and easier way to do it?

Thanks!


回答1:


Another option:

library(lubridate)
video$video_duration <- as.numeric(video_end - video_begin, units = "secs")
video$video_duration  <- seconds_to_period(video$video_duration)

          video_begin           video_end video_duration
1 2017-09-12 00:08:14 2017-09-12 00:39:08        30M 54S
2 2017-09-12 00:04:47 2017-09-12 00:47:10        42M 23S
3 2017-09-12 00:08:27 2017-09-12 00:49:51        41M 24S
4 2017-09-12 00:04:59 2017-09-12 00:44:31        39M 32S
5 2017-09-12 00:04:57 2017-09-12 00:39:41        34M 44S
6 2017-09-12 00:07:51 2017-09-12 00:47:12        39M 21S
7 2017-09-12 00:06:11 2017-09-12 00:40:13         34M 2S
8 2017-09-12 00:05:30 2017-09-12 00:46:52        41M 22S



回答2:


This creates MM:SS format:

df$video_duration <- gsub('\\s+[[:alpha:]]{4}','',df$video_duration)
x <- as.numeric(df$video_duration)
paste(floor(x), round((x-floor(x))*60), sep=":")

Output:

[1] "30:54" "42:23" "41:24" "39:32" "34:44" "39:21" "34:2"  "41:22"



回答3:


Using difftime wrapped in a function. There may be a more elegant solution but hey it works and is flexible.

time_diff <- function(time1, time2){
  hours <- difftime(time1, time2, units="hours")
  minutes <- round(60 * (hours - floor(hours)), 0)

  paste(floor(hours), minutes, sep=":")
}
time_diff(video_end, video_begin)

gives

[1] "0:31" "0:42" "0:41" "0:40" "0:35" "0:39" "0:34" "0:41"


来源:https://stackoverflow.com/questions/46167536/how-to-convert-decimal-time-to-time-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!