我有以下数据(DF)查找时间差
Id Timestamp Event
1 2015-11-06 06:11:43 mail subscribed
1 2015-11-06 06:15:43 Invoice created
1 2015-11-06 09:15:43 phone call
2 2015-11-07 08:15:43 New subscription
2 2015-11-07 08:20:43 Added to customer list.
我找下面(每一个ID时差)
例如,ID = 1有三个不同的时间不同的事件,我想计算基于Id的事件之间的各个时间之间的差异。
Id Timestamp Event Time Difference(Mins)
1 2015-11-06 06:11:43 mail subscribed 0.0
1 2015-11-06 06:15:43 Invoice created 5.0
1 2015-11-06 09:15:43 phone call 180.0
2 2015-11-07 08:15:43 New subscription 0.0
2 2015-11-07 08:20:43 Added to customer list 5.0
我尝试下面的代码,
diff = function(x) as.numeric(x - lag(x))
or diff = function (x) as.numeric(0L,diff(x))
setDT(df)[, diff2 := diff(timestamp), by = Id]
但这代码输出不规则的结果。任何帮助?
不清楚。你想获得什么? – nicola
也许你可以尝试'difftime'? – Jaap
@nicola我更新了我的问题 – Maddy