I am trying to generate SessionId
from UserId
and difference in timestamps
. The first user in the table is obviously assigned 1 and from there the sessionId has to be incremented by 1 if the same user logs in and the difference between current login and previous login is less than 5 minutes else it has to be incremented by 1. I am able to do this using for
loop and nested if
statements but wondering if it can be done by using dplyr
or similar packages. There is already a similar question here but it sorts the UserId's and I want to achieve this without sorting UserId's.
Input and Output(SessionId) is in the same table.
CustomerID TimeStamp SessionID
101 0000-01-01 01:00:00 1
101 0000-01-01 01:03:00 1
102 0000-01-01 01:05:00 2
103 0000-01-01 01:06:00 3
104 0000-01-01 01:06:00 4
101 0000-01-01 01:09:00 5
105 0000-01-01 01:10:00 6
104 0000-01-01 01:10:00 4
106 0000-01-01 01:11:00 7
105 0000-01-01 01:12:00 6
104 0000-01-01 01:12:00 4
104 0000-01-01 01:18:00 8
104 0000-01-01 01:20:00 8
105 0000-01-01 01:21:00 9
104 0000-01-01 01:26:00 10