【发布时间】:2017-06-09 11:23:24
【问题描述】:
我有一个数据框
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))))
【问题讨论】:
-
您的意思是
soDf %>% filter((Voucher.Number == lag(Voucher.Number)) & (State == 'Exception' & lag(State) == 'Exception' ))...您需要致电filter以便...好吧..过滤您的数据 -
@Sotos 我没有包含后面的代码,但我使用
filter过滤行 == True。在这一点上,我有兴趣看到结果......好吧,因为我对此感兴趣。 -
在您的问题代码中,只需将
mutate(toFilt = ....)替换为filter(((User == ...)))
标签: r dataframe filter dplyr conditional-statements