Вот базовая идея R,
i1 <- cumsum(c(TRUE, diff(d2$daynight) != 0))
do.call(rbind,
Filter(function(x)!is.null(x),
tapply(d2$mydate, i1, FUN = function(i)if(length(i) >= 3){c(i[1], i[length(i)])})))
, которая дает,
[,1] [,2]
4 "2018-11-22" "2018-11-30"
8 "2018-12-05" "2018-12-11"
ДАННЫЕ
dput(d2)
structure(list(mydate = c("2018-11-21", "2018-11-21", "2018-11-22",
"2018-11-22", "2018-11-23", "2018-11-24", "2018-11-25", "2018-11-26",
"2018-11-27", "2018-11-28", "2018-11-29", "2018-11-30", "2018-12-01",
"2018-12-04", "2018-12-05", "2018-12-05", "2018-12-08", "2018-12-09",
"2018-12-10", "2018-12-11", "2018-12-12"), daynight = c(1L, 2L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L,
2L, 2L, 1L), myvar = c(64L, 4L, 7L, 2L, 0L, 0L, 64L, 6L, 0L,
0L, 20L, 4L, 64L, 1L, 4L, 0L, 30L, 15L, 0L, 14L, 28L)), row.names = c("195",
"196", "197", "198", "199", "200", "201", "202", "203", "204",
"205", "206", "207", "214", "215", "216", "217", "218", "219",
"220", "221"), class = "data.frame")