我需要基於按時間順序排列的對象(此處爲日期)創建一個值序列(在下面的數據框中命名爲「seq」)。要建立一個新的序列,兩個日期之間的時間間隔需要嚴格大於1小時。基於按時間順序排列的對象創建值序列
下面是一個例子
ID date seq
A 2010-04-14 02:00:12 1
A 2010-04-14 02:00:12 1
A 2010-04-14 03:00:10 1
A 2010-04-14 03:00:10 1
A 2010-04-14 04:00:15 1
A 2010-04-14 04:00:15 1
A 2010-04-14 08:00:10 2
A 2010-04-14 08:00:10 2
B 2010-04-14 03:00:18 3
B 2010-04-14 03:00:18 3
B 2010-04-14 04:00:10 3
B 2010-04-14 04:00:10 3
B 2010-04-14 10:00:14 4
B 2010-04-14 10:00:14 4
B 2010-04-14 11:00:10 4
B 2010-04-14 11:00:10 4
數據
tab <- data.frame(ID= rep(c("A","B"), each=8), date= as.POSIXct(c('2010-04-14 02:00:12','2010-04-14 02:00:12','2010-04-14 03:00:10', '2010-04-14 03:00:10','2010-04-14 04:00:15','2010-04-14 04:00:15','2010-04-14 08:00:10','2010-04-14 08:00:10','2010-04-14 03:00:18','2010-04-14 03:00:18','2010-04-14 04:00:10','2010-04-14 04:00:10','2010-04-14 10:00:14','2010-04-14 10:00:14','2010-04-14 11:00:10','2010-04-14 11:00:10'), format='%Y-%m-%d %H:%M:%S'))
像'1L + cumsum(DIFF(標籤$日期)> 60 * 60)' – Frank