【发布时间】:2020-12-22 07:56:28
【问题描述】:
我有一个像下面这样的数据框
ItemNo OrderAmount Date
3845 320 2020-01-21
3245 -100 2020-01-20
4045 -200 2020-01-20
3845 300 2020-01-19
3845 320 2020-01-18
3245 100 2020-01-18
3645 230 2020-01-18
3645 -230 2020-01-18
3245 -100 2020-01-18
3845 320 2020-01-17
4045 0 2020-01-17
3845 320 2020-01-17
3845 -300 2020-01-17
3245 200 2020-01-17
3645 230 2020-01-16
4045 200 2020-01-15
3845 -300 2020-01-15
3245 100 2020-01-15
3245 100 2020-01-15
3845 320 2020-01-15
4045 240 2020-01-15
4045 0 2020-01-15
我希望匹配一个组(ItemNo)中的负数和正数,然后从数据框中删除匹配的行。如果 OrderAmount 为 0,则将其保存在数据框中。我希望输出是
ItemNo OrderAmount Date
3845 320 2020-01-21
3845 320 2020-01-18
3245 100 2020-01-18
3645 230 2020-01-18
3845 320 2020-01-17
4045 0 2020-01-17
3845 320 2020-01-17
3845 -300 2020-01-17
3245 200 2020-01-17
3845 320 2020-01-15
4045 240 2020-01-15
4045 0 2020-01-15
我尝试过使用:
# Dataframe
DF <- data.frame(ItemNo=c(3845,3245,4045,3845,3845,3245,3645,3645,3245,3845,4045,3845,3845,3245,3645,4045,3845,3245,3245,3845,4045,4045),
OrderAmount = c(320,-100,-200,300,320,100,230,-230,-100,320,0,320,-300,200,230,200,-300,100,100,320,240,0),
Date = c("2020-01-21","2020-01-20","2020-01-20","2020-01-19","2020-01-18","2020-01-18","2020-01-18","2020-01-18","2020-01-18","2020-01-17","2020-01-17","2020-01-17","2020-01-17","2020-01-17","2020-01-16","2020-01-15","2020-01-15","2020-01-15","2020-01-15","2020-01-15","2020-01-15","2020-01-15"))
DF$Date <- as.Date(DF$Date)
# Order by date -> oldest first
DF <- DF[order(desc(DF$Date)),]
DF %>%
group_by(ItemNo, nvalue = abs(OrderAmount)) %>%
filter(!duplicated(OrderAmount)) %>%
filter(sum(OrderAmount) >= 0) %>%
ungroup() %>%
select(-nvalue)
例如我希望 ItemNo 3245 的输出为
ItemNo OrderAmount Date
3245 -100 2020-01-20 #DELETE: Matched with 5. Row
3245 100 2020-01-18
3245 -100 2020-01-18 #DELETE: Matched with 6. Row
3245 200 2020-01-17
3245 100 2020-01-15 #DELETE: Matched with 1. Row
3245 100 2020-01-15 #DELETE: Matched with 3. Row
【问题讨论】:
-
请准确描述匹配模式应该是什么。例如。我不清楚为什么输入数据框中的 4. 行被删除。另外,请提供:stackoverflow.com/help/minimal-reproducible-example
-
第4行与第17行匹配,因为它在同一组中,并且是相同的OrderAmount,符号相反。 OrderAmount 需要在同一组内进行一对一匹配,符号相反。