Reputation: 109
I would like to remove rows that are not fulfilling the condition that I want. For example:
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
If per event, in the column of value there is a number higher than 2 (Value > 2) remove the first 3 rows starting from that Value that is not fulfilling the criteria.
It should look like this:
Event Value
1 1
1 0
1 0
1 0
2 0
2 0
3 0
3 0
I have been able to remove the first row of each Event that accomplish the criteria, but haven't find a way to remove the other rows.
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)
A %>%
group_by(Event) %>%
filter(!(Value >= 2 & row_number() == 1))
Any idea?
Upvotes: 2
Views: 785
Reputation: 887118
Here is an option with slice
. After grouping by 'Event', check if
there are any
'Value' greater than or equal to 2, then return the negative index of the sequence from the first occurrence of that case or else
return the row_number()
. The negative index removes those rows
library(dplyr)
A %>%
group_by(Event) %>%
slice(if(any(Value >=2)) -(which(Value >=2)[1] + 0:2) else row_number())
Upvotes: 2