【问题标题】:How to convert decimal time to time format [duplicate]如何将十进制时间转换为时间格式[重复]
【发布时间】:2017-09-12 03:41:23
【问题描述】:

我想计算两个时间集之间的差异。我可以这样做,但我只能得到小数的差异,我想知道如何将它们转换为“分钟:秒”中的格式。

所以,我将分钟和秒作为字符:

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

我用 as.POSIXct 将它们转换为时间值,制作一个 df 并将差异添加为第三列,很容易...

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

这就是我得到的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

如何将video$video_duration 的格式从十进制更改为与video$video_begin 和video$video_end 相同的格式:“Minutes:Seconds”(我不关心日、月、年和小时)?

我试过了:

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

和

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

但是不...

我找到了一些答案,但我对它们不太满意:

How convert decimal to POSIX time

Algorithm to convert Text time to Decimal Time

难道没有更...​​方便且更简单的方法吗?

谢谢!

【问题讨论】:

    标签: r time decimal lubridate strptime


    【解决方案1】:

    另一种选择:

    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】:

    这将创建 MM:SS 格式:

    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=":")
    

    输出:

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

    【讨论】:

    • 谢谢,这可能有效,但我正在寻找一个涉及更少编码的答案。我之前找到的其他答案也很有效,但它们都不是很“方便”。还是谢谢你!
    【解决方案3】:

    使用包装在函数中的difftime。可能有更优雅的解决方案,但它有效且灵活。

    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)
    

    给予

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

    【讨论】:

    • 这可能行得通,但老实说,这对我来说有点太多了! :) 还是谢谢你!
    猜你喜欢
    • 2021-08-04
    • 2017-09-03
    • 2014-03-16
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 2014-03-13
    • 2016-05-09
    相关资源
    最近更新 更多