Here's the basic R option using rle :
df$dummy <- with(rle(df$temp > 5), rep(as.integer(values & lengths >= 10), lengths))
Some explanation: The task is a classic use case for the length encoding ( rle ) function, imo. First, check if temp 5 is greater (creating a logical vector) and apply rle to this vector, as a result we get:
> rle(df$temp > 5)
Now we want to find those cases where values are TRUE (i.e., the tempo is greater than 5) and where at the same time lengths greater than 10 (i.e. at least ten consecutive temp values ββare greater than 5). We do this by running:
values & lengths >= 10
And finally, since we want to return a vector of the same length as nrow(df) , we use rep(..., lengths) and as.integer to return 1/0 instead of TRUE / FALSE .
source share