Q:

How to remove all negative events that last for 6 hours or more in dataframe, R

For now I only know how to remove all negative values, but I have a dataframe which I would like to remove only the negative values that are showing coherent events e.g. coherent negative values for a certain time period: 6 hours or more
Here is an example of the dataframe 
              date         dtoh
270 2013-02-01 19:00:00 -406577438
271 2013-02-01 21:00:00 25065969896
272 2013-02-01 23:00:00 34957370054
273 2013-02-02 01:00:00 34496956567
274 2013-02-02 03:00:00 21044877275
275 2013-02-02 05:00:00 46403458579
276 2013-02-02 07:00:00 27750883451
277 2013-02-02 09:00:00 24171409206
278 2013-02-02 11:00:00 23125727882
279 2013-02-02 13:00:00 6714467434
280 2013-02-02 15:00:00 7977663655
281 2013-02-02 17:00:00 -6244316045
282 2013-02-02 19:00:00 -7543932473
283 2013-02-02 21:00:00 -4259783871
284 2013-02-02 23:00:00 NaN
285 2013-02-12 13:00:00 45996800517
286 2013-02-12 15:00:00 44378536161
287 2013-02-12 17:00:00 37007195908
288 2013-02-12 19:00:00 34462565230
289 2013-02-12 21:00:00 39266264220
290 2013-02-12 23:00:00 36583277497
291 2013-02-13 01:00:00 33437451940
292 2013-02-13 03:00:00 42263115311
293 2013-02-13 05:00:00 40998819449
294 2013-02-13 07:00:00 42024903440
295 2013-02-13 09:00:00 8224799549
296 2013-02-13 11:00:00 -37747691614
297 2013-02-13 13:00:00 -23293257192
298 2013-02-13 15:00:00 -18158938873
299 2013-02-13 17:00:00 -6004332415
300 2013-02-13 19:00:00 -6036261025
301 2013-02-13 21:00:00 8646978420
302 2013-02-13 23:00:00 8442921517

The expected output where I have deleted all negative events that last for 6 hours or more: 
           date           dtoh
269 2013-02-01 17:00:00 30416525109
270 2013-02-01 19:00:00 -406577438
271 2013-02-01 21:00:00 25065969896
272 2013-02-01 23:00:00 34957370054
273 2013-02-02 01:00:00 34496956567
274 2013-02-02 03:00:00 21044877275
275 2013-02-02 05:00:00 46403458579
276 2013-02-02 07:00:00 27750883451
277 2013-02-02 09:00:00 24171409206
278 2013-02-02 11:00:00 23125727882
279 2013-02-02 13:00:00 6714467434
280 2013-02-02 15:00:00 7977663655
284 2013-02-02 23:00:00 NaN
285 2013-02-12 13:00:00 45996800517
286 2013-02-12 15:00:00 44378536161
287 2013-02-12 17:00:00 37007195908
288 2013-02-12 19:00:00 34462565230
289 2013-02-12 21:00:00 39266264220
290 2013-02-12 23:00:00 36583277497
291 2013-02-13 01:00:00 33437451940
292 2013-02-13 03:00:00 42263115311
293 2013-02-13 05:00:00 40998819449
294 2013-02-13 07:00:00 42024903440
295 2013-02-13 09:00:00 8224799549
301 2013-02-13 21:00:00 8646978420
302 2013-02-13 23:00:00 8442921517

I really appreciate your help!

A:

Using rleid from the data.table package, you can group the data by runs of positive/negative values. Then, filter out negative rows where the group timespan is 6 or more. I'm assuming that an event ends when the next one begins.
library(dplyr)

df %>%
  mutate(date_end = lead(date, default = last(date))) %>%
  group_by(group = data.table::rleid(dtoh < 0)) %>%
  filter(is.na(dtoh) | dtoh > 0 | difftime(max(date_end), min(date), units = "hours") < 6) %>%
  ungroup()

