You can also use the rleid
function from the data.table
package which is specifically designed for generating a run-length type id column in grouping operations:
library(data.table)
setDT(df)[, Seq := rleid(LogTime), by=IDPath]
which gives:
> df
IDPath LogTime Seq
1: AADS 19-06-2015:01:57 1
2: AADS 19-06-2015:01:55 2
3: AADS 19-06-2015:01:54 3
4: AADS 19-06-2015:01:53 4
5: DHSD 19-06-2015:12:57 1
6: DHSD 19-06-2015:10:58 2
7: DHSD 19-06-2015:09:08 3
8: DHSD 19-06-2015:08:41 4
Another option would be to use the rowid
function:
setDT(df)[, Seq := rowid(IDPath)]