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:
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!
CodePudding user response:
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)