Re: Map conditional sums by date
- To: mathgroup at smc.vnet.net
- Subject: [mg99862] Re: [mg99849] Map conditional sums by date
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Sat, 16 May 2009 18:24:38 -0400 (EDT)
- Reply-to: hanlonr at cox.net
This gets more complicated if you want to be able to simultaneously select multiple years, months, days, and/or codes. 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`} }; months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; codes = Union[mydata[[All, 2]]]; Manipulate[condTotal = Total[Select[mydata, (Not[yrQ] || #[[1, 1]] == yr) && (Not[moQ] || #[[1, 2]] == Position[months, mo][[1, 1]]) && (Not[ dayQ] || #[[1, 3]] == day) && (Not[codeQ] || #[[2]] == code) &][[All, 3]]], {{yrQ, False, "Year"}, {True, False}}, {{yr, DateList[][[1]], ""}, Range @@ {Min[#], Max[#]} &[mydata[[All, 1, 1]]]}, {{moQ, False, "Month"}, {True, False}}, {{mo, "Jan", ""}, months}, {{dayQ, False, "Day"}, {True, False}}, {{day, 1, ""}, Range[31]}, {{codeQ, False, "Code"}, {True, False}}, {{code, codes[[1]], ""}, codes}] Dynamic[condTotal] 26.79 Bob Hanlon ---- "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!