I am working with a dataframe where I have sequential time data for an animal moving around in a room. I have a column denoting when the animal is immobile (isImmobile = TRUE or FALSE) and I would like to create a new column showing the duration of the immobile episodes - in the below example it would be 3 frames - so I can exclude the ones that are too short.
I would also want to include a column which can tell me the number of immobile episodes.
Here is some example data:
time speed zone animal group week distance period isImmobile
1 5.26 73.8 interzone ctrl5 ctrl 0 50.3 p1 FALSE
2 5.29 70.8 interzone ctrl5 ctrl 0 1.30 p1 FALSE
3 5.33 70 interzone ctrl5 ctrl 0 1.40 p1 TRUE
4 5.36 56.3 interzone ctrl5 ctrl 0 1.98 p1 TRUE
5 5.39 51.7 interzone ctrl5 ctrl 0 2.70 p1 TRUE
6 5.43 47 interzone ctrl5 ctrl 0 1.84 p1 FALSE
What I'm looking for is something like this:
time speed zone animal group week distance period isImmobile Ep_number Ep _Duration
1 5.26 73.8 interzone ctrl5 ctrl 0 50.3 p1 FALSE NA NA
2 5.29 70.8 interzone ctrl5 ctrl 0 1.30 p1 FALSE NA NA
3 5.33 70 interzone ctrl5 ctrl 0 1.40 p1 TRUE 1 3
4 5.36 56.3 interzone ctrl5 ctrl 0 1.98 p1 TRUE 1 3
5 5.39 51.7 interzone ctrl5 ctrl 0 2.70 p1 TRUE 1 3
6 5.43 47 interzone ctrl5 ctrl 0 1.84 p1 FALSE NA NA
The frame can also completely omit the episodes when isImmobile = FALSE rather than having NA.
Preferably dplyr but anything else is also good :)
Thank you so much in advance. I also hope a similar question hasn't been asked yet - I couldn't find anything.