带有单位的 POSIXct 的差异,例如在 difftime [英] Diff for POSIXct with units, like in difftime

查看:25
本文介绍了带有单位的 POSIXct 的差异,例如在 difftime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 POSIXct 数据向量,想计算连续元素之间的差异,就像 diff

例如:

burst <- as.POSIXct(c("2016-11-07 17:20:52","2016-11-07 16:21:52", "2016-11-07 15:21:52"、"2016-11-02 17:20:52"、"2016-11-02 16:21:52"、"2016-11-02 15:21:52"))conti <- as.POSIXct(c("2016-11-07 17:20:52","2016-11-06 17:20:52", "2016-11-05 17:20:52", "2016-11-04 17:20:52"、"2016-11-03 17:20:52"、"2016-11-02 17:20:52"))差异(突发)差异(连续)

我的问题是,单位不相等.我记得函数 difftime 有一个名为 unit 的参数,但我无法构造一个 apply 函数来获得类似的行为在 diff 中.

解决方案

我不确定是否有办法使用 diff 为 POSIXt 对象强制执行特定单元.如果没有,您可以直接使用数值,然后设置类和单位属性,以便您仍然拥有一个带有单位属性的 difftime 对象.

例如,要获得以分钟为单位的差异:

burst.diff = diff(as.numeric(burst))/60burst.diff = as.difftime(burst.diff, units="mins")突发.diff

<块引用>

以分钟为单位的时差[1] -59 -60 -7141 -59 -60

如果你不关心维护对象类或单元,那么你可以这样做:

burst.diff = diff(as.numeric(burst))/60

你可以把它打包成一个函数.我不确定这是否是我处理类和属性的正确"方式,我很想知道是否有更好的方法来做到这一点:

my_difftime = function(x, units="mins") {div = c("secs"=1, "mins"=60, "hours"=3600)if(is.na(match(units, names(div)))) {stop('请将单位指定为秒"、分钟"或小时"')} 别的 {x = diff(as.numeric(x))/div[match(units, names(div))]as.difftime(x, 单位=单位)}}lapply(list(burst=burst, conti=conti), my_difftime, 单位=小时")

<块引用>

$burst以小时为单位的时差[1] -0.9833333 -1.0000000 -119.0166667 -0.9833333 -1.0000000$conti以小时为单位的时差[1] -24 -25 -24 -24 -24

I have got a vector of POSIXct-data and want to calculate the difference between consecutive elements, like it is done by diff

e.g:

burst <- as.POSIXct(c("2016-11-07 17:20:52","2016-11-07 16:21:52", "2016-11-07 15:21:52", "2016-11-02 17:20:52","2016-11-02 16:21:52", "2016-11-02 15:21:52"))

conti <- as.POSIXct(c("2016-11-07 17:20:52","2016-11-06 17:20:52", "2016-11-05 17:20:52", "2016-11-04 17:20:52","2016-11-03 17:20:52", "2016-11-02 17:20:52"))

diff(burst)
diff(conti)

My problem is, that the units are not equal. I remember the function difftime which has got a parameter called unit but i am not able to construct a apply-function to get a simiral behaviour like in diff.

解决方案

I'm not sure if there's a way to enforce particular units with diff for POSIXt objects. In case there isn't, you can work directly with the numerical values and then set the class and the units attribute so that you'll still have a difftime object with a units attribute.

For example, to get differences in minutes:

burst.diff = diff(as.numeric(burst))/60
burst.diff = as.difftime(burst.diff, units="mins")

burst.diff

Time differences in mins
[1]   -59   -60 -7141   -59   -60

If you don't care about maintaining the object class or units, then you can just do:

burst.diff = diff(as.numeric(burst))/60 

You could package this as a function. I'm not sure if this is the "right" way in terms of how I've handled class and attributes and I'd be interested to know if there are better ways to do this:

my_difftime = function(x, units="mins") {

  div = c("secs"=1, "mins"=60, "hours"=3600)

  if(is.na(match(units, names(div)))) {
    stop('Please specify either units as either "secs", "mins", or "hours"')
  } else {
    x = diff(as.numeric(x))/div[match(units, names(div))]
    as.difftime(x, units=units) 
  }
}

lapply(list(burst=burst, conti=conti), my_difftime, units="hours")

$burst
Time differences in hours
[1]   -0.9833333   -1.0000000 -119.0166667   -0.9833333   -1.0000000

$conti
Time differences in hours
[1] -24 -25 -24 -24 -24

这篇关于带有单位的 POSIXct 的差异,例如在 difftime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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