This will take you a step closer
library(tidyverse)
# Sample data on a copy/paste friendly format
df <- data.frame(
time = c(1,2,3,4,5,6,7,8,9,10,11,12,13,
14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
30),
value = c(10,20,34,45,55,66,77,55,28,12,23,
43,56,67,89,54,32,11,18,24,28,37,45,54,59,67,41,
32,15,2)
)
df %>%
mutate(helper_col = value - lag(value)) %>%
filter(helper_col >= 0) %>%
filter(value > 20 & value < 60) %>%
select(-helper_col) %>%
mutate(group = if_else(time - lag(time) > 1 | is.na(lag(time)), row_number(), NA_integer_)) %>%
fill(group, .direction = "down") %>%
group_split(group)
#> [[1]]
#> # A tibble: 3 x 3
#> time value group
#> <dbl> <dbl> <int>
#> 1 3 34 1
#> 2 4 45 1
#> 3 5 55 1
#>
#> [[2]]
#> # A tibble: 3 x 3
#> time value group
#> <dbl> <dbl> <int>
#> 1 11 23 4
#> 2 12 43 4
#> 3 13 56 4
#>
#> [[3]]
#> # A tibble: 6 x 3
#> time value group
#> <dbl> <dbl> <int>
#> 1 20 24 7
#> 2 21 28 7
#> 3 22 37 7
#> 4 23 45 7
#> 5 24 54 7
#> 6 25 59 7
#>
#> attr(,"ptype")
#> # A tibble: 0 x 3
#> # … with 3 variables: time <dbl>, value <dbl>, group <int>
Created on 2020-03-13 by the reprex package (v0.3.0.9001)