问题描述
以lubridate
的指定单位获取interval
表示的时间长度的最佳方法是什么?我能弄清楚的是类似以下杂乱的事情:
What's the best way to get the length of time represented by an interval
in lubridate
, in specified units? All I can figure out is something like the following messy thing:
> ival
[1] 2011-01-01 03:00:46 -- 2011-10-21 18:33:44
> difftime(attr(ival, "start") + as.numeric(ival), attr(ival, "start"), 'days')
Time difference of 293.6479 days
(我也在 https://github.com/hadley/lubridate/issues/105 ,但前提是没有更好的方法可用-但也许这里有人知道一个.)
(I also added this as a feature request at https://github.com/hadley/lubridate/issues/105, under the assumption that there's no better way available - but maybe someone here knows of one.)
更新-显然difftime
函数也不能处理此问题.这是一个例子.
Update - apparently the difftime
function doesn't handle this either. Here's an example.
> (d1 <- as.POSIXct("2011-03-12 12:00:00", 'America/Chicago'))
[1] "2011-03-12 12:00:00 CST"
> (d2 <- d1 + days(1)) # Gives desired result
[1] "2011-03-13 12:00:00 CDT"
> (i2 <- d2 - d1)
[1] 2011-03-12 12:00:00 -- 2011-03-13 12:00:00
> difftime(attr(i2, "start") + as.numeric(i2), attr(i2, "start"), 'days')
Time difference of 23 hours
正如我在下面提到的,我认为处理此问题的一种好方法是实现/.interval
函数,该函数不会首先将其输入转换为period
.
As I mention below, I think one nice way to handle this would be to implement a /.interval
function that doesn't first cast its input to a period
.
推荐答案
lubridate提供的功能是as.duration
.间隔类在内部以从开始算起的秒数表示,因此,如果需要小时数,可以将as.numeric(ival)
除以3600,或者除以(3600 * 24)天.
The as.duration
function is what lubridate provides. The interval class is represented internally as the number of seconds from the start, so if you wanted the number of hours you could simply divide as.numeric(ival)
by 3600, or by (3600*24) for days.
如果要将适用的函数示例应用于您的对象,则应提供dput(ival
的输出.我对?interval
发送给我的help(duration)
页面上创建的对象进行了测试.
If you want worked examples of functions applied to your object, you should provide the output of dput(ival
). I did my testing on the objects created on the help(duration)
page which is where ?interval
sent me.
date <- as.POSIXct("2009-03-08 01:59:59") # DST boundary
date2 <- as.POSIXct("2000-02-29 12:00:00")
span <- date2 - date #creates interval
span
#[1] 2000-02-29 12:00:00 -- 2009-03-08 01:59:59
str(span)
#Classes 'interval', 'numeric' atomic [1:1] 2.85e+08
# ..- attr(*, "start")= POSIXct[1:1], format: "2000-02-29 12:00:00"
as.duration(span)
#[1] 284651999s (9.02y)
as.numeric(span)/(3600*24)
#[1] 3294.583
# A check against the messy method:
difftime(attr(span, "start") + as.numeric(span), attr(span, "start"), 'days')
# Time difference of 3294.583 days
这篇关于润滑间隔时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!