如何根据先前的行值更新列值?

How do I update the column values based on previous row values?

数据框看起来像这样:

Date        |   Length .

2005-01-01  -   3400 . 

2005-01-02  -    54000 .

2005-01-03  -     266450 .

2005-01-04  -     0 . 

2005-01-05  -     0 . 

我想分解“266450”中的值并将 86400 分配到下一行,如果该值超过 86400,则再次将剩余的值传递到下一行

例如,答案应该是这样的

Date        |   Length .

2005-01-01  -   3400 . 

2005-01-02  -    54000 .

2005-01-03  -     86400 .

2005-01-04  -     86400 . 

2005-01-05  -     86400 .

2005-01-06 -      7250  

一个可重现的例子将有助于理解具体问题,但我认为这里主要和最有趣的问题是如何 "split" 和 "spread" 向量上的数字。您可以使用整数商和 remainder/modulus 函数(%%%/%)来执行此操作。例如:

#function to split a number by some other number
# and spread the integer quotients and remainders
# over vector elements
split_and_spread <- function(x, BY){
 c(rep(BY, x %/% BY), x %% BY)
}

split_and_spread(266450, BY = 86400)
#[1] 86400 86400 86400  7250

并使用 (sapplyunlist)

应用于向量
x = c(3400, 54000, 266450)

#call with sapply and unlist
unlist(sapply(x, split_and_spread, BY = 86400))
#[1]  3400 54000 86400 86400 86400  7250