【问题标题】:Maintain date time stamp when calculating time intervals计算时间间隔时保持日期时间戳
【发布时间】:2017-01-24 01:34:04
【问题描述】:

我根据location 和sensor 计算了日期和时间之间的时间间隔。这是我的一些数据:

datehour <- c("2016-03-24 20","2016-03-24 06","2016-03-24 18","2016-03-24 07","2016-03-24 16",
          "2016-03-24 09","2016-03-24 15","2016-03-24 09","2016-03-24 20","2016-03-24 05",
          "2016-03-25 21","2016-03-25 07","2016-03-25 19","2016-03-25 09","2016-03-25 12",
          "2016-03-25 07","2016-03-25 18","2016-03-25 08","2016-03-25 16","2016-03-25 09",
          "2016-03-26 20","2016-03-26 06","2016-03-26 18","2016-03-26 07","2016-03-26 16",
          "2016-03-26 09","2016-03-26 15","2016-03-26 09","2016-03-26 20","2016-03-26 05",
          "2016-03-27 21","2016-03-27 07","2016-03-27 19","2016-03-27 09","2016-03-27 12",
          "2016-03-27 07","2016-03-27 18","2016-03-27 08","2016-03-27 16","2016-03-27 09")
location <- c(1,1,2,2,3,3,4,4,"out","out",1,1,2,2,3,3,4,4,"out","out",
              1,1,2,2,3,3,4,4,"out","out",1,1,2,2,3,3,4,4,"out","out")
sensor <- c(1,16,1,16,1,16,1,16,1,16,1,16,1,16,1,16,1,16,1,16,
            1,16,1,16,1,16,1,16,1,16,1,16,1,16,1,16,1,16,1,16)
Temp <- c(35,34,92,42,21,47,37,42,63,12,35,34,92,42,21,47,37,42,63,12,
          35,34,92,42,21,47,37,42,63,12,35,34,92,42,21,47,37,42,63,12)
df <- data.frame(datehour,location,sensor,Temp)

我使用以下代码计算时间差。但是,它不会为每个条目维护正确的日期时间。请参阅列 datehour1 和 datehour2。

df$datehour <- as.POSIXct(df$datehour, format = "%Y-%m-%d %H")
final.time.df <- setDT(df)[order(datehour, location, sensor), .(difftime(datehour[-length(datehour)], 
                                                                            datehour[-1], unit = "hour"), 
                                                                            datehour1 = datehour[1], datehour2 = datehour[2]), .(location, sensor)]

我希望每个时间差都使用两次来计算以识别它。我希望结果如下:

    location sensor        V1           datehour1           datehour2
  out     16 -28 hours 2016-03-24 05:00:00 2016-03-25 09:00:00
    1     16 -25 hours 2016-03-24 06:00:00 2016-03-25 07:00:00
    2     16 -26 hours 2016-03-24 07:00:00 2016-03-25 09:00:00
    3     16 -22 hours 2016-03-24 09:00:00 2016-03-25 07:00:00
    4     16 -23 hours 2016-03-24 09:00:00 2016-03-25 08:00:00
    4      1 -27 hours 2016-03-24 15:00:00 2016-03-25 18:00:00
    3      1 -20 hours 2016-03-24 16:00:00 2016-03-25 12:00:00
    2      1 -25 hours 2016-03-24 18:00:00 2016-03-25 19:00:00
    1      1 -25 hours 2016-03-24 20:00:00 2016-03-25 21:00:00
  out      1 -20 hours 2016-03-24 20:00:00 2016-03-25 16:00:00

【问题讨论】:

    标签: r time data.table time-series


    【解决方案1】:

    好的,所以我无论如何都不是data.tables 解决方案的专家,因此我不太确定您如何使用分组语句将值的数量减少到 10。

    也就是说,我认为您的问题的答案(如果您还没有以另一种方式解决)在于difftime(datehour[-length(datehour)], datehour[-1], unit = "hour") 代码块,但不是因为它计算的差异不正确,而是在于它阻止分组语句解析为预期的组数。

    我尝试将分组与时差计算分开,并且能够得到您预期的输出(显然需要一些格式):

    final.time.df <- setDT(df)[order(datehour, location, sensor), .(datehour1 = datehour[1], datehour2 = datehour[2]), .(location, sensor)]
    final.time.df$diff = final.time.df$datehour1 - final.time.df$datehour2
    

    如果我错过了重点,请随时告诉我,我会删除答案!我知道这不是一个特别有见地的答案,但看起来这可能会做到这一点,而且我自己现在陷入了一个问题,并想尝试提供帮助。

    【讨论】:

    • 我花了一点时间才回到这个问题上。您的解决方案帮助我以不同的方式思考问题并找到更好的方法。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    相关资源
    最近更新 更多