设置日期时间对象之间的差异单位 [英] Set units of difference between datetime objects

查看:41
本文介绍了设置日期时间对象之间的差异单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

diff 命令以 R 日期格式返回日期向量中日期之间的差异.我想控制返回的单位,但它们似乎是自动确定的,无法通过参数进行控制.举个例子:

The diff command returns the differences between dates in a vector of dates in the R date format. I'd like to control the units that are returned, but it seems like they are automatically determined, with no way to control it w/ an argument. Here's an example:

 > t = Sys.time() 
 > diff(c(t, t + 1))
 Time difference of 1 secs

然而:

> diff(c(t, t+10000))
Time difference of 1.157407 days

时间增量"对象有一个单位属性,但写一堆条件来将所有内容强制为天、秒等似乎很愚蠢.

The "time delta" object has a units attribute, but it seems silly to write a bunch of conditionals to coerce everything into days, seconds etc.

推荐答案

我不知道你说的一堆条件"是什么意思,只需手动更改单位.

I'm not sure what you mean by "a bunch of conditionals," just change the units manually.

> t = Sys.time()
> a <- diff(c(t,t+1))
> b <- diff(c(t, t+10000))
> units(a) <- "mins"
> units(b) <- "mins"
> a
Time difference of 0.01666667 mins
> b
Time difference of 166.6667 mins

参见?difftime.如果您只需要使用 diff 来获得两次之间的差异(而不是更长的向量),那么,正如 Dirk 建议的那样,使用 difftime 函数和 units 参数.

See ?difftime. If you only need to use diff to get the difference between two times (rather than a longer vector), then, as Dirk suggests, use the difftime function with the units parameter.

这篇关于设置日期时间对象之间的差异单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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