Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to join distinct episodes within a specific time-frame

I am trying to calculate the number of episodes where the animal was immobile in a task. I would like to set a threshold for how long an immobility episode needs to be broken for in order to be considered mobile.

In the below example, you can see that the animal was considered to have multiple individual immobile episodes (Ep_number) within a very short time-frame (episodes 4 and 5 and episodes 6 and 7 occur within milliseconds from each other). I would like to set a condition where if the time difference between the end of the first episode and the start of the next episode is less than 3 seconds, the animal is considered immobile throughout.

Here is example data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     time      zone   animal group  week  Ep_number
1: 169.222    open      a1    g1     0         4
2: 169.254    open      a1    g1     0         4
3: 169.322    open      a1    g1     0         5
4: 197.418   closed     a1    g1     0         6
5: 197.451   closed     a1    g1     0         6
6: 197.484   closed     a1    g1     0         6
7: 198.684   closed     a1    g1     0         7
8: 198.718   closed     a1    g1     0         7
9: 198.751   closed     a1    g1     0         7

This is how it would look if correct:

     time      zone   animal group  week  Ep_number
1: 169.222    open      a1    g1     0         4
2: 169.254    open      a1    g1     0         4
3: 169.322    open      a1    g1     0         4
4: 197.418   closed     a1    g1     0         5
5: 197.451   closed     a1    g1     0         5
6: 197.484   closed     a1    g1     0         5
7: 198.684   closed     a1    g1     0         5
8: 198.718   closed     a1    g1     0         5
9: 198.751   closed     a1    g1     0         5

Thank you!

>Solution :

Here’s a base R solution :

df <- read.table(h=TRUE, text = "
     time      zone   animal group  week  Ep_number
169.222    open      a1    g1     0         4
169.254    open      a1    g1     0         4
169.322    open      a1    g1     0         5
197.418   closed     a1    g1     0         6
197.451   closed     a1    g1     0         6
197.484   closed     a1    g1     0         6
198.684   closed     a1    g1     0         7
198.718   closed     a1    g1     0         7
198.751   closed     a1    g1     0         7")

df$Ep_number <- df$Ep_number[1] + cumsum(c(0, diff(df$time)) > 3)
df
#>      time   zone animal group week Ep_number
#> 1 169.222   open     a1    g1    0         4
#> 2 169.254   open     a1    g1    0         4
#> 3 169.322   open     a1    g1    0         4
#> 4 197.418 closed     a1    g1    0         5
#> 5 197.451 closed     a1    g1    0         5
#> 6 197.484 closed     a1    g1    0         5
#> 7 198.684 closed     a1    g1    0         5
#> 8 198.718 closed     a1    g1    0         5
#> 9 198.751 closed     a1    g1    0         5

Created on 2022-04-20 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading