R,反向累积和

时间:2017-07-31 18:45:55

标签: r cumsum

假设我们有两个表:

预算表:

 Item       Date Expense
    A 2017-08-24     850
    B 2017-08-18     300
    B 2017-08-11      50
    C 2017-08-18      50
    C 2017-08-11     100
    D 2017-08-01     500

expDF = structure(list(Item = c("A", "B", "B", "C", "C", "D"), Date = structure(c(17402, 
17396, 17389, 17396, 17389, 17379), class = "Date"), Expense = c(850L, 
300L, 50L, 50L, 100L, 500L)), .Names = c("Item", "Date", "Expense"
), row.names = c(NA, -6L), class = "data.frame")

每个日期按项目列出的预计费用表。

Item    Date        Spend
A       8/24/2017   850
B       8/18/2017   300
B       8/11/2017   50
C       8/18/2017   50
C       8/11/2017   50
D       8/1/2017    0

我希望总结每个日期每件商品可以支付的金额,如下所示:

PS

1 个答案:

答案 0 :(得分:1)

这有效:

library(data.table)
setDT(bDF); setDT(expDF)

expDF[bDF, on=.(Item), Spending :=
  pmin(
    Expense, 
    pmax(
      0, 
      Budget - cumsum(shift(Expense, fill=0))
    )
  )
, by=.EACHI]

   Item       Date Expense Spending
1:    A 2017-08-24     850      850
2:    B 2017-08-18     300      300
3:    B 2017-08-11      50       50
4:    C 2017-08-18      50       50
5:    C 2017-08-11     100       50
6:    D 2017-08-01     500        0

工作原理

  • cumsum(shift(Expense, fill = 0))之前的消费**
  • max(0,预算 - 先前支出)是剩余预算
  • min(费用,剩余预算)是当前支出

data.table语法x[i, on=, j, by=.EACHI]是一个连接。在这种情况下,j采用v := expr形式,向x添加新列。有关详细信息,请参阅?data.table

**嗯,"先前"在订购表。我会忽略OP的奇怪的逆转日期。