计算个体内各行之间的日期时间间隔之和 (R)

Computing the sum of datetime intervals between rows within individuals (R)

我想改变一个按 ID 分组的新列,并总结每个会话之间的间隔。间隔定义为 End 时间与其后续 Start 时间之间的差异。例如,对于 ID 1,这将是 (2021-07-11 05:55:00 - 2021-07-11 01:14:00 = 281 分钟) 和 (2021-07-11 11:08:00 - 2021-07-11 08:09:00 = 179 分钟) 的总和,即 460。

df <- structure(list(ID = c(1, 1, 1, 2, 2, 2), Start = structure(c(1625931780, 
1625954100, 1625972880, 1625505720, 1625517480, 1625526900), class = c("POSIXct", 
"POSIXt"), tzone = "Singapore"), End = structure(c(1625937240, 
1625962140, 1625981580, 1625513640, 1625523300, 1625531880), class = c("POSIXct", 
"POSIXt"), tzone = "Singapore"), n = c(3L, 3L, 3L, 3L, 3L, 3L
)), row.names = c(NA, 6L), class = "data.frame")

  ID               Start                 End n
1  1 2021-07-10 23:43:00 2021-07-11 01:14:00 3
2  1 2021-07-11 05:55:00 2021-07-11 08:09:00 3
3  1 2021-07-11 11:08:00 2021-07-11 13:33:00 3
4  2 2021-07-06 01:22:00 2021-07-06 03:34:00 3
5  2 2021-07-06 04:38:00 2021-07-06 06:15:00 3
6  2 2021-07-06 07:15:00 2021-07-06 08:38:00 3

期望:

  ID               Start                 End n sumIntervals
1  1 2021-07-10 23:43:00 2021-07-11 01:14:00 3          460
2  1 2021-07-11 05:55:00 2021-07-11 08:09:00 3          460
3  1 2021-07-11 11:08:00 2021-07-11 13:33:00 3          460
4  2 2021-07-06 01:22:00 2021-07-06 03:34:00 3          124
5  2 2021-07-06 04:38:00 2021-07-06 06:15:00 3          124
6  2 2021-07-06 07:15:00 2021-07-06 08:38:00 3          124

注意: 开始和结束在 POSIXct 中,每个 ID 中的会话数是不是常量,所以它可以是 n 的任意数量。这里用n=3来说明。

任何帮助将不胜感激!

我们可以在 'ID' 分组后取 'Start' 的 lead,使用 difftime 得到 'mins' 与 [=23] 的差异=] 和 sum integer 转换后的值

library(dplyr)
df <- df %>%
      group_by(ID) %>%
      mutate(new = sum(as.integer(difftime(lead(Start), End, 
             units = 'mins')), na.rm = TRUE) ) %>%
      ungroup

-输出

# A tibble: 6 x 5
     ID Start               End                     n   new
  <dbl> <dttm>              <dttm>              <int> <int>
1     1 2021-07-10 23:43:00 2021-07-11 01:14:00     3   460
2     1 2021-07-11 05:55:00 2021-07-11 08:09:00     3   460
3     1 2021-07-11 11:08:00 2021-07-11 13:33:00     3   460
4     2 2021-07-06 01:22:00 2021-07-06 03:34:00     3   124
5     2 2021-07-06 04:38:00 2021-07-06 06:15:00     3   124
6     2 2021-07-06 07:15:00 2021-07-06 08:38:00     3   124