计算时间间隔时保持日期时间戳

时间:2017-01-24 01:34:04

标签: r time data.table time-series

我根据locationsensor计算了日期和时间之间的时间间隔。以下是我的一些数据:

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)

我使用以下代码计算时差。但是,它不会保持每个条目的正确日期时间。请参阅列datehour1datehour2

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

1 个答案:

答案 0 :(得分: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

如果我错过了这一点,请随时告诉我,我会删除答案!我知道这不是一个特别富有洞察力的答案,但它看起来可能会这样做,而且我现在自己也遇到了问题,并希望尝试提供帮助。