Q:

Removing a group with conditional statement in r

The conditional statement is that in any event, if there are two or more consecutive rows with values higher than 1, the group should be deleted.
For example:
Event<- c(1,1,1,1,2,2,2,2,2,2,3,3,3,3,3)
Value<- c(1,0,0,0,8,7,1,0,0,0,8,0,0,0,0)
A<- data.frame(Event, Value)

Event   Value
  1       1
  1       0
  1       0
  1       0
  2       8
  2       7
  2       1
  2       0
  2       0
  2       0
  3       8
  3       0
  3       0
  3       0
  3       0

In this example the group of event 2 should be deleted because it has two consecutive rows with  values higher than 1. So it should looks like:
Event   Value
  1       1
  1       0
  1       0
  1       0
  3       8
  3       0
  3       0
  3       0
  3       0

Any suggestion?

A:

We can use rle by groups. 
library(dplyr)
A %>%
 group_by(Event) %>%
 filter(!any(with(rle(Value > 1), lengths[values] > 1)))
 #Opposite way using all
 #filter(all(with(rle(Value > 1), lengths[values] < 2)))

#  Event Value
#  <dbl> <dbl>
#1     1     1
#2     1     0
#3     1     0
#4     1     0
#5     3     8
#6     3     0
#7     3     0
#8     3     0
#9     3     0

The same logic can be used in base R : 
subset(A, !ave(Value > 1, Event, FUN = function(x) 
               any(with(rle(x), lengths[values] > 1))))

as well as data.table
library(data.table)
setDT(A)[, .SD[!any(with(rle(Value > 1), lengths[values] > 1))], Event]

