Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...