繁体   English   中英

使用dplyr过滤行

[英]Using dplyr to filter rows

我有一个数据框

soDf <- structure(list(State = c("Exception", "Exception", "Exception",  "Exception", "Approval", "Processing"), User = c("1","2", "1", "3", "1", "4"), Voucher.Number = c(10304685L, 10304685L, 10304685L,10304685L, 10304685L, 10304685L),  Queue.Exit.Date = c("8/24/2016 14:59", "8/26/2016 13:25", "8/26/2016 15:56", "8/26/2016 16:13", "8/26/2016 16:25", "8/26/2016 17:34")),.Names = c("State", "User", "Voucher.Number","Queue.Exit.Date"), row.names = 114:119, class = "data.frame")

我有一个规则列表,我希望通过这些规则来过滤行:

规则之一是

(Voucher.Number == lag(Voucher.Number)) & (State == 'Exception' & lag(State) == 'Exception' )

如果当前凭证和滞后凭证的编号相等,并且都具有异常标记,则将该行标记为True

当我将此规则与其他一对夫妇返回第4行作为True时,它应该返回False

       State User Voucher.Number Queue.Exit.Date toFilt
1  Exception    1       10304685 8/24/2016 14:59     NA
2  Exception    2       10304685 8/26/2016 13:25   TRUE
3  Exception    1       10304685 8/26/2016 15:56   TRUE
4  Exception    3       10304685 8/26/2016 16:13   TRUE
5   Approval    1       10304685 8/26/2016 16:25  FALSE
6 Processing    4       10304685 8/26/2016 17:34  FALSE

这是我与所有过滤规则一起使用的代码

soDf <- soDf %>%
  arrange(Voucher.Number, Queue.Exit.Date)%>%
  mutate(toFilt =  ((User == lag(User)& Voucher.Number ==lag(Voucher.Number)))|
           ((Voucher.Number != lag(Voucher.Number)) & State == "Exception") |
           ((Voucher.Number == lag(Voucher.Number)) & (State == 'Exception' & lag(State) == 'Exception' ))|
           ((Voucher.Number == lag(Voucher.Number)) & (User == lag(User))))  

第5行不符合您在mutate列中的条件语句。 第5行的状态是“批准”,而不是“例外”,并且用户ID与滞后的用户ID不匹配。

由于这个原因,它返回FALSE,因为这4条语句都不是TRUE。 它似乎不是编码错误,只是条件语句需要更改以符合您的需求。 希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM