[英]delete values if followed by certain value in R
I am fairly new to R and I would need your help. 我对R很陌生,需要您的帮助。 I have a dataset, which looks like the following example:
我有一个数据集,看起来像下面的例子:
id type timespan
1 yes 2016-07-30 06:22:24
1 no 2016-07-30 09:12:16
1 yes 2016-07-30 10:42:20
2 no 2016-07-30 18:46:15
1 yes 2016-07-30 19:56:54
2 yes 2016-07-30 20:44:00
I would now like to keep only those "yes" values that are not followed by a "no" value based on the ID. 我现在只想保留基于ID的“ yes”值,然后不跟“ no”值。 This is how I would expect my output to be:
这就是我期望的输出结果:
id type timespan
1 yes 2016-07-30 10:42:20
1 yes 2016-07-30 19:56:54
2 yes 2016-07-30 20:44:00
Is there a way how to do this in R? 有没有办法在R中执行此操作?
Thank you for your help! 谢谢您的帮助!
Something like: 就像是:
library(dplyr)
df %>%
group_by(id) %>%
filter(type == 'yes' & coalesce(lead(type) != 'no', T))
In data.table
, you could do: 在
data.table
,您可以执行以下操作:
library(data.table)
setDT(df)[, .SD[(type != "no" & (shift(type, type = "lead") != "no" | is.na(shift(type, type = "lead"))))], by = id]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.