如何获得具有多个单位的输出的时间差 [英] How to get time differences with output having multiple units

查看:16
本文介绍了如何获得具有多个单位的输出的时间差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,

我在 as.POSIXct 中有两个日期列,格式为 YYYY-MM-DD HH:MM:SS.我想得到两者之间的差异,以天小时:秒的格式显示.这是一些虚拟数据:

I have two date columns in as.POSIXct in the format YYYY-MM-DD HH:MM:SS. I would like to get the difference between the two, displayed in the format Days Hours:Seconds. Here is some dummy data:

    a<-c("2018-03-20 11:52:25 AST", "2018-03-20 12:51:25 AST", "2018-03-20 14:19:04 AST",
"2018-03-21 14:12:12 AST", "2018-03-21 12:09:22 AST", "2018-03-21 15:28:01 AST")

b<-c("2018-04-09 18:39:38 AST", "2018-06-23 19:13:14 AST", "2018-03-20 23:23:03 AST",
     "2018-05-10 21:29:28 AST", "2018-03-22 03:17:23 AST", "2018-05-12 00:19:39 AST")

ab<-data.frame(a,b)

给出了这个数据框:

                        a                       b
 2018-03-20 11:52:25 AST 2018-04-09 18:39:38 AST
 2018-03-20 12:51:25 AST 2018-06-23 19:13:14 AST
 2018-03-20 14:19:04 AST 2018-03-20 23:23:03 AST
 2018-03-21 14:12:12 AST 2018-05-10 21:29:28 AST
 2018-03-21 12:09:22 AST 2018-03-22 03:17:23 AST
 2018-03-21 15:28:01 AST 2018-05-12 00:19:39 AST

我想得到 a 和 b 之间的差异,或者从时间 b 中减去时间 a 以获得 X 天 X 小时:X 秒的输出.

I would like to get the difference between a and b, or subtract time a from time b to get an output of X days X hours: X seconds.

我在下面使用了 difftime,以及不同设置的单位:

I have used difftime below, along with the units set differently:

ab$time_difference<-difftime(ab$b, ab$a)
ab
                            a                       b   time_difference
     2018-03-20 11:52:25 AST 2018-04-09 18:39:38 AST  486.786944 hours
     2018-03-20 12:51:25 AST 2018-06-23 19:13:14 AST 2286.363611 hours
     2018-03-20 14:19:04 AST 2018-03-20 23:23:03 AST    9.066389 hours
     2018-03-21 14:12:12 AST 2018-05-10 21:29:28 AST 1207.287778 hours
     2018-03-21 12:09:22 AST 2018-03-22 03:17:23 AST   15.133611 hours
     2018-03-21 15:28:01 AST 2018-05-12 00:19:39 AST 1232.860556 hours

我还尝试了以下方法:

ab$time_difference<-difftime(ab$b, ab$a,units=c("days","hours","seconds"))

但是得到单位"长度必须为 1 的错误.我应该使用不同的命令,还是有什么方法可以让 difftime 产生更精确的时差?

But get the error that 'units' must be a length of 1. Is there a different command I should be using, or is there any way for difftime to produce a more exact time difference?

谢谢!

推荐答案

既然你想要天、小时、分钟、秒,我们可以用 lubridate 包得到这个结果:

Since you would like days, hours, minutes, seconds, we can get this result with the lubridate package:

a<-c("2018-03-20 11:52:25 AST", "2018-03-20 12:51:25 AST", "2018-03-20 14:19:04 AST",
 "2018-03-21 14:12:12 AST", "2018-03-21 12:09:22 AST", "2018-03-21 15:28:01 AST")

b<-c("2018-04-09 18:39:38 AST", "2018-06-23 19:13:14 AST", "2018-03-20 23:23:03 AST",
 "2018-05-10 21:29:28 AST", "2018-03-22 03:17:23 AST", "2018-05-12 00:19:39 AST")

a = as.POSIXct(a)
b = as.POSIXct(b)

library(lubridate)
timespan = interval(ymd_hms(ab[,1]), ymd_hms(ab[,2]))
> as.period(timespan)
[1] "20d 6H 47M 13S"    "3m 3d 6H 21M 49S"  "9H 3M 59S"         "1m 19d 7H 17M 16S"
[5] "15H 8M 1S"         "1m 20d 8H 51M 38S"

如果需要,我们可以通过指定格式将月转换为天:

If desired, we can convert months to days by specifying the formatting as follows:

> as.period(timespan, unit = "day")
[1] "20d 6H 47M 13S" "95d 6H 21M 49S" "9H 3M 59S"      "50d 7H 17M 16S"
[5] "15H 8M 1S"      "51d 8H 51M 38S"

这篇关于如何获得具有多个单位的输出的时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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