I would like to add 1 to session
column when the difference between the first timestamp
of the same session and followings timestamp
records is more than 10 units.
in other words:
if the gap in timestamp
column is more than 10 in the same session, add 1 to the rest of the sessions for a specific ID. So we shouldn't have the same session
with the gap more than 10 in its records.
lets say:
df<-read.table(text="
ID timestamp session
1 10 1
1 12 1
1 15 1
1 21 1
1 25 1
1 27 2
1 29 2
2 11 1
2 22 2
2 27 2
2 32 2
2 42 2
2 43 3",header=T,stringsAsFactors = F)
In the example above, for ID==1
the session gap from the first record (timestamp==10) is more than 10 in row 4 (timestamp==21), so we add 1 to the rest of sessions. Whenever the session number change the difference of the first record of timestamp should be less than 10 in the same sassion, otherwise it should add to session.
result:
ID timestamp session
1 *10 1
1 12 1
1 15 1
1 *21 2 <-- because 21-10 >= 10 it add 1 to the rest of sessions in this ID
1 25 2
1 27 3
1 29 3
2 11 1
2 *22 2
2 27 2
2 *32 3 <-- because 32-22>= 10 it add 1 to the rest of session
2 *42 4 <-- because 42-32>=10
2 43 5
How can I do it in R?