簡體   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