Re: Map conditional sums by date
- To: mathgroup at smc.vnet.net
- Subject: [mg99866] Re: [mg99849] Map conditional sums by date
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Sat, 16 May 2009 18:25:23 -0400 (EDT)
- References: <200905160920.FAA26816@smc.vnet.net>
Hi Charles, the following will do what you want and more, since the first setting of each slider is "All", which allows you to total over the full range of items this slider is responsible for (day, month or year), and thus you can decide if you want AND or OR logic for each item, and therefore be as specific or as general as you wish. Clear[getUI]; getUI[data_] := Module[{months, monthSelect, yearSelect, daySelect, totalize, years, days}, months = {All, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; years = Prepend[Union@data[[All, 1, 1]], All]; With[{part = #2}, #1[arg_] := Select[#, #[[1, part]] == arg &] &] & @@@ Transpose[{{yearSelect, monthSelect, daySelect}, {1, 2, 3}}]; daySelect[0] = yearSelect[All] = monthSelect[0] = # &; totalize = Total[#[[All, 3]]] &; days = Prepend[Range[31], All]; Manipulate[ Grid[{{"Year", "Month", "Day", "Amount"}, {years[[j]], months[[i]], days[[k]], totalize@ yearSelect[years[[j]]]@ monthSelect[i - 1]@daySelect[k - 1]@data}}, Frame -> All], {{i, 1, "Month"}, 1, 13, 1}, {{j, 1, "Year"}, 1, Length[years], 1}, {{k, 1, "Day"}, 1, Length[days], 1}, Alignment -> Center]]; Usage: getUI[mydata], with <mydata> from your e-mail. Note that I made explicit use of your specific format and therefore you have to maintain it for this to work. The main idea of this implementation is that 3 selector functions monthSelect, yearSelect, daySelect select the corresponding records and pass the resulting structure to the next selector function in the chain. If any of them receives the <All> parameter, it simply stays idle and passes the entire data to the next selector function. It shouldn't be hard to attach a barchart-generating code to this if needed. Hope this helps. Regards, Leonid On Sat, May 16, 2009 at 2:20 AM, Charles L. Snyder <clsnyder at gmail.com>wrote: > Hi > > I want to (ultimately) use Manipulate to provide date selections > (year, months, days) to create a BarChart. I am able to get the > results from my data in a kludgy way. The data is in the form {{yr, > mo, day, h,min,sec}, code, amount}, as seen below: > > mydata={{2009, 2, 5, 0, 0, 0}, 54161, 3.27`}, {{2006, 8, 23, 0, 0, > 0}, 54163, 3}, {{2007, 12, 5, 0, 0, 0}, 43280, 17.25`}, {{2009, 2, > 5, > 0, 0, 0}, 54161, 3.27`}} > > I want to conditionally total the amounts (3rd column), by yr OR by > month OR by day; which I can do with : > > Select[mydata, #[[1, 2]] == 7 &] > (* would give me all the rows in which July (7th mo) is the month, for > example*) > OR > Select[mydata, #[[1, 1]] == 2008 &] > (* would give me all the rows in which 2008 is the year *) > > I can sum the amounts with: > Total[#[[3]] & /@ %] > > I need basic help with putting these into a function, but ultimately I > want to be able to select yr or mo or day of week via Manipulate and > get the total.... > > My failed attempts: > > (* parameters are yrmoday -> 1 is year, 2 is mo, 3 is day, and x is > the specific year or month or day*) > > sumbydate[x_, yrmoday_] := > If[#[[1, yrmoday]] == x, myresult += #[[3]], myresult += 0] & /@ > mydata; myresult > > OR > > (* this would be sum by month, if it worked...) > > sumbydate[x_, yrmoday_] := If[#[[1, yrmoday]] == x, res += #[[3]], res > += 0] & /@ temp; Map[ > sumrvubydate2[#] &, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}] > > Thanks! > >
- References:
- Map conditional sums by date
- From: "Charles L. Snyder" <clsnyder@gmail.com>
- Map conditional sums by date