I have a set of observations, with nest IDs and date of the observation. I have created a variable that gives the interval between each observation for each nest. I need to create a new variable, that for each nest, assigns a unique value for each observation that falls within an interval period of 145 days. Essentially, a shortened version of my data looks like this:
Date Nest_ID interval total_interval
1 2015-02-22 a 0 0
2 2015-05-26 a 93 93
3 2015-07-08 a 43 136
4 2015-12-07 a 152 288
5 2016-01-29 a 53 341
6 2016-04-02 a 64 405
7 2016-07-06 a 95 500
8 2016-11-05 a 122 622
9 2017-06-29 b 0 0
10 2015-02-22 c 0 0
and I need it to look like this:
Date Nest_ID interval total_interval Nest_period
1 2015-02-22 a 0 0 1
2 2015-05-26 a 93 93 1
3 2015-07-08 a 43 136 1
4 2015-12-07 a 152 288 2
5 2016-01-29 a 53 341 2
6 2016-04-02 a 64 405 2
7 2016-07-06 a 95 500 3
8 2016-11-05 a 122 622 3
9 2017-06-29 b 0 0 1
10 2015-02-22 c 0 0 1
If I could get the total_interval count to restart whenever it reaches 145 that seems like the ideal solution, however I have not been able to figure that out yet.
Here's some code to get the sample data:
Date <- c("2015-02-22", "2015-05-26", "2015-07-08", "2015-12-07", "2016-01-29", "2016-04-02", "2016-07-06", "2016-11-05", "2017-06-29", "2015-02-22")
Nest_ID <- c("a", "a", "a", "a", "a", "a", "a", "a", "b", "c")
interval <- (c(0,93,43,152,53,64,95,122,0,0))
total_interval <- (c(0,93,136,288,341,405,500,622,0,0))
df <- as.data.frame(cbind(Date, Nest_ID, interval, total_interval))
Any help greatly appreciated! Thanks!