I hope the function in the snippet below will get you started. I invented some data to work with. I did not invent different sites or seasons because I was too lazy. I your real data, you will want to group_by() all the appropriate variables.
The first thing I do is construct a datetime column so that time differences can be calculated.
What the function does is filter for the desired sal values and then calculate the time between neighboring rows. If that time is 15, then the rows are part of a continuous run. The rle function looks at the values in the Diff column and characterizes the run length of all the values. We are interested in runs of the value 15 and my code finds the longest such run.
library(dplyr)
library(lubridate)
#Invent data. Don't worry about the details here
Sequence <- seq.POSIXt(from = ymd_hm("2022-01-01 00:00"),
to = ymd_hm("2022-02-28 00:00"), by = "15 min")
DATA <- data.frame(year = 2022, month = month(Sequence), day = day(Sequence),
time = paste0(formatC(hour(Sequence),width = 2, flag = "0"),
formatC(minute(Sequence), width = 2, flag = "0")),
sal = rnorm(5569,40,5))
#Construct a POSIX datetime from the year, month etc.
DATA <- DATA |> mutate(DateTime = make_datetime(year,month,day,
hour=as.numeric(substr(time,1,2)),
min=as.numeric(substr(time,3,4))))
FindRun <- function(DF){
DF40_50 <- DF |> filter(between(sal, 40,50)) |>
mutate(Lag = lag(DateTime),
Diff = as.numeric(DateTime - Lag))
RUNs <- rle(DF40_50$Diff)
Lengths <- RUNs$lengths[RUNs$values==15]
MaxRun <- max(Lengths,na.rm = TRUE)
MaxRun *15/1440
}
FindRun(DATA)
#> [1] 0.07291667
Created on 2022-06-06 by the reprex package (v2.0.1)