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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…