How to filter rows based on the previous row and keep previous row using dplyr?

无人久伴 提交于 2021-02-15 07:50:40

问题


I am trying to subset rows of a data set using a condition that's based on the previous row, whilst keeping the previous row in the subsetted data. This is essentially the same as the question here, but I am looking for a dplyr approach:

Select specific rows based on previous row value (in the same column)

I have taken the dplyr approach applied in the comments to that answer, but I am unable to figure out the last step of retaining the previous row.

I can get the rows that support the condition I'm interested in (incorrect when the previous row is not enter).

set.seed(123)
x=c("enter","incorrect","enter","correct","incorrect",
"enter","correct","enter","incorrect")
y=c(runif(9, 5.0, 7.5))
z=data.frame(x,y)

filter(z, x=="incorrect" & lag(x)!="enter")

Which gives, as expected:

      x        y
1 incorrect 7.351168 

What I would like to produce is this, so that all rows I've filtered based on the condition are stored with the row that precedes them in the original data set:

        x        y
1   correct 7.207544
2 incorrect 7.351168

Any help would be greatly appreciated!


回答1:


By filtering you could do:

z %>%
  filter( (x == "incorrect" & lag(x) != "enter") | lead(x == "incorrect" & lag(x) != "enter") )

Giving:

          x        y
1   correct 7.207544
2 incorrect 7.351168


来源:https://stackoverflow.com/questions/54348952/how-to-filter-rows-based-on-the-previous-row-and-keep-previous-row-using-dplyr

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