# List has a date and amount, I need to create a cumulative amount by date

I have a list which has a date and a amount, I need to create a list that has the cummulative total with the date.

I have the following result:

{{{2009, 8, 3}, 1829.}, {{2009, 8, 4}, 1113.}, {{2009, 8, 5}, 730.}, {{2009, 8, 6}, -243.}}


What I need:

 {{{2009, 8, 3}, 1829.}, {{2009, 8, 4}, 2942.}, {{2009, 8, 5}, 3672.}, {{2009, 8, 6}, 3429.}}


I have just spent over an hour trying to figure this out, any help is most appreciated.

-

list = {{{2009, 8, 3}, 1829.}, {{2009, 8, 4}, 1113.}, {{2009, 8, 5},730.}, {{2009, 8, 6}, -243.}}

Rest@FoldList[{#2[[1]], (#1 + #2)[[2]]} &, 0, list]


Or

Rest@FoldList[{#2[[1]], Last@Plus@## } &, 0, list]

-
Thank you, they both work great. Since I have worked with FoldList before I will go with this one. – John Jun 8 '12 at 21:54
@John Transpose and Accumulate (both used in b.gatessucks' answer) are very useful functions. If you are not using them yet, you should :) – Dr. belisarius Jun 9 '12 at 3:15

Starting with:

list = {{{2009, 8, 3}, 1829.}, {{2009, 8, 4}, 1113.}, {{2009, 8, 5}, 730.}, {{2009, 8, 6}, -243.}};


I propose:

MapAt[Accumulate, list\[Transpose], 2]\[Transpose]


Which in the Notebook looks like:

Or with in-place modification:

list[[All, 2]] = Accumulate @ list[[All, 2]]; list

-

Something like :

alist = {{{2009, 8, 3}, 1829.}, {{2009, 8, 4}, 1113.}, {{2009, 8, 5},730.}, {{2009, 8, 6}, -243.}};

Transpose[{alist[[All, 1]], Accumulate[alist[[All, 2]]]}]

-
@acheong87 Running my code reproduces the requested output; do you get anything different ? – b.gatessucks Aug 25 '13 at 7:51
My mistake; somehow I was getting a list of dates, and a list of cumulative values; my input must have gotten mucked up somewhere. Deleting my comment; already +1'ed. – Andrew Cheong Aug 26 '13 at 2:53