R 先前计算函数的滞后值

R Lag value of previous calculated function

我正在尝试使用前一行的滞后值,需要从前一行计算(除非它的第一个条目)。

我正在尝试类似于:

test<-data.frame(account_id=c(123,123,123,123,444,444,444,444),entry=c(1,2,3,4,1,2,3,4),beginning_balance=c(100,0,0,0,200,0,0,0),
                 deposit=c(10,20,5,8,10,12,20,4),running_balance=c(0,0,0,0,0,0,0,0))

test2<-test %>%
  group_by(account_id) %>%
  mutate(running_balance = if_else(entry==1, beginning_balance+deposit,
                                   lag(running_balance)+deposit))

print(test2)

运行 余额应为 110,130,135,143,210,222,242,246

对于每个 account_id,您可以添加 first beginning_balance,累计总和为 deposit

library(dplyr)

test %>%
  group_by(account_id) %>%
  mutate(running_balance = first(beginning_balance) + cumsum(deposit))


#  account_id entry beginning_balance deposit running_balance
#       <dbl> <dbl>             <dbl>   <dbl>           <dbl>
#1        123     1               100      10             110
#2        123     2                 0      20             130
#3        123     3                 0       5             135
#4        123     4                 0       8             143
#5        444     1               200      10             210
#6        444     2                 0      12             222
#7        444     3                 0      20             242
#8        444     4                 0       4             246

使用 data.table 同样的事情:

library(data.table)
setDT(test)[, running_balance := first(beginning_balance) + cumsum(deposit), account_id]

对每个唯一 account_id 使用 for 循环并为每个 id 添加累积和。

for ( i in unique (test$account_id)) {

  test$running_balance [test$account_id == i] <- cumsum(test$beginning_balance[test$account_id == i]+test$deposit[test$account_id == i])

}

print (test)

     account_id entry beginning_balance deposit running_balance
1        123     1               100      10             110
2        123     2                 0      20             130
3        123     3                 0       5             135
4        123     4                 0       8             143
5        444     1               200      10             210
6        444     2                 0      12             222
7        444     3                 0      20             242
8        444     4                 0       4             246