I have the following data:
test1<- data.frame("ID"= c("1","1","1", "2","2","2","3"), "TimeElapsed"=c(0,10,40,0,40,12,0))
Looks like:
ID TimeElapsed
1 0
1 10
1 40
2 0
2 40
2 12
3 0
I want to create bins/sessions such that whenever time elapsed since last entry of an ID is more than 30, I want to start a new session for that ID. I use the following code from dplyr.
test1<-test1 %>%
group_by(ID) %>%
mutate(cut_session = (TimeElapsed > 30) + 1)
I get this result:
ID TimeElapsed Session
1 0 1
1 10 1
1 40 2
2 0 1
2 40 2
2 12 1 <- Doesn't work for this case
3 0 1
I would like a solution that gives me session= 2 in the second last row since time elapsed is <30, it should be the same session. Many thanks in advance.