Conditional loop writing for big data

@davis and @thoughtfulnz,
I solved the above problem but I can not transfer data from previous group to the next group dynamically. I need to use the data in next group which does not follow the conditions. I tried the following code.

tt %>%
  mutate(date_chunk = floor_date(Time.x - offset_for_floor, "5 mins") + 
                                     offset_for_floor)%>%
  group_by(date_chunk)%>%
  mutate( min.value = min(UNIX_Time.x),
          max.value = min.value + 300,
          T= case_when(
            T2<=min.value | T1>=max.value ~ max.value - min.value,
            T2<=min.value | T1<=max.value ~ T1 - min.value,
            T2>=min.value | T1>=max.value ~ max.value - T2,
            T2>=min.value | T1<=max.value ~ T1- T2),
            T2>=max.value | T1>=max.value ~ 0,
)

Do you have any suggestions?