How to loop according to a multiple of a value

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!

I started down a couple of dead ends, so this is probably far from optimal. Notice I changed the code to define df.

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 <- data.frame(Date, Nest_ID, interval, total_interval)
library(dplyr)
PeriodFunc <- function(VALS) {
  Init <- VALS[1]
  Indx <- 1
  Period <- 1
  PeriodVec <- rep(0,length(VALS))
  
  while(Indx <= length(VALS)){
    while(((VALS[Indx] - Init) < 145) && Indx <= length(VALS)) {
      PeriodVec[Indx] <- Period
      Indx <- Indx + 1
    }
    if(Indx <= length(VALS)){
      Period <- Period + 1
      PeriodVec[Indx] <- Period
      Init <- VALS[Indx]
      Indx <- Indx + 1
    }
  }
  PeriodVec
}
df %>% group_by(Nest_ID) %>% mutate(PeriodLabel = PeriodFunc(total_interval))
#> # A tibble: 10 x 5
#> # Groups:   Nest_ID [3]
#>    Date       Nest_ID interval total_interval PeriodLabel
#>    <chr>      <chr>      <dbl>          <dbl>       <dbl>
#>  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

Created on 2021-09-15 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.