removing the first 3 rows of a group with conditional statement in r

喜夏-厌秋 提交于 2021-01-27 19:10:35

问题


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?


回答1:


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())


来源:https://stackoverflow.com/questions/61417517/removing-the-first-3-rows-of-a-group-with-conditional-statement-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!