[R] running balance down a dataframe referring back to previous row

David Katz david at davidkatzconsulting.com
Wed Mar 19 16:42:39 CET 2008


Try:

 cs <- with(txns,cumsum(cr - dr))

You could if needed adjust the starting value to zero by concatenating a
zero in front and dropping the last entry.

txns$running.bal <- c(0,cs[seq(length(cs) - 1)])

Good luck.


seanpor wrote:
> 
> Good morning, I've searched high and low and I've tried many different
> ways
> of doing this, but I can't seem to get it to work.
> 
> I'm looking for a way of vectorising a "running balance"; i.e. the value
> in
> the first row of the dataframe is zero, and following rows add to this
> running balance.  This is easy to write in a loop, but I can't seem to get
> it working in vectorised code.  Hopefully the example below will explain
> what I'm trying to do...
> 
> Many thanks in advance,
> 
> Best regards,
> Sean O'Riordain
> 
> 

-- 
View this message in context: http://www.nabble.com/running-balance-down-a-dataframe-referring-back-to-previous-row-tp16142263p16145133.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list