如何根据前一行过滤行并使用 dplyr 保留前一行? [英] How to filter rows based on the previous row and keep previous row using dplyr?

查看:42
本文介绍了如何根据前一行过滤行并使用 dplyr 保留前一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用基于前一行的条件对数据集的行进行子集化,同时将前一行保留在子集化数据中.这与此处的问题基本相同,但我正在寻找一种 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:

选择特定行基于前一行值(在同一列中)

我采用了对该答案的评论中应用的 dplyr 方法,但我无法弄清楚保留前一行的最后一步.

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.

我可以获得支持我感兴趣的条件的行(不正确,当前一行不是enter时).

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

正如预期的那样:

      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

任何帮助将不胜感激!

推荐答案

通过过滤你可以:

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

给予:

          x        y
1   correct 7.207544
2 incorrect 7.351168

这篇关于如何根据前一行过滤行并使用 dplyr 保留前一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆