简体   繁体   中英

Using dplyr to filter rows

I have a dataframe

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

I have a list of rules that I want to filter rows by:

One of the rules being

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

If the current and lag voucher number are equal, and both have an exception tag, then count mark that row as True .

When I apply this rule with a couple other it returns the 4th row as True when it should be returned as 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

Here is the code I used with all of the filtering rules

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

Line 5 does not meet your conditional statement in the mutate column. The state of line 5 is "Approval" as opposed to "Exception", and the User ID does not match the lagged user ID.

For this reason, it returns FALSE as none of the 4 statements are TRUE. It does not appear to be a coding error just the conditional statement needs altering to match your needs. Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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