Мы можем делать с группой по операции (при условии, что DateTime преобразуется в datetime
класс)
library(dplyr)
library(tidyr)
df1 %>%
mutate(DateTime = as.POSIXct(DateTime, format = "%m/%d/%Y %H:%M")) %>%
group_by(Year, Month) %>%
summarise(Consecutive = list(rle(cumsum(c(TRUE,
diff(DateTime, unit = "hour") != 1)))$lengths)) %>%
unnest
data
df1 <- structure(list(Year = c(1950L, 1950L, 1950L, 1950L, 1950L, 1950L,
1950L, 1950L), Month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Day = c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 5L), Hour = c(12L, 13L, 14L, 15L, 18L,
21L, 22L, 23L), DateTime = c("1/4/1950 12:00", "1/4/1950 13:00",
"1/4/1950 14:00", "1/4/1950 15:00", "1/4/1950 18:00", "1/4/1950 21:00",
"1/4/1950 22:00", "1/5/1950 23:00")), class = "data.frame",
row.names = c(NA, -8L))