Just a little background: I got into programming through statistics, and I don’t have much formal programming experience, I just know how to make things work. I’m open to any suggestions to come at this from a differet direction, but I’m currently using multiple sqldf queries to get my desired data. I originally started statistical programming in SAS and one of the things I used on a regular basis was the macro programing ability.
For a simplistic example say that I have my table A as the following:
Name Sex A B DateAdded
John M 72 1476 01/14/12
Sue F 44 3269 02/09/12
Liz F 90 7130 01/01/12
Steve M 21 3161 02/29/12
The select statement that I’m currently using is of the form:
sqldf("SELECT AVG(A), SUM(B) FROM A WHERE DateAdded >= '2012-01-01' AND DateAdded <= '2012-01-31'")
Now I’d like to run this same query on the enteries where DateAdded is in the Month of February. From my experience with SAS, you would create macro variables for the values of DateAdded. I’ve considered running this as a (very very slow) for loop, but I’m not sure how to pass an R variable into the sqldf, or whether that’s even possible. In my table, I’m using the same query over years worth of data–any way to streamline my code would be much appreciated.
Read in the data, convert the
DateAddedcolumn toDateclass, add ayearmon(year/month) column and then usesqldforaggregateto aggregate by year/month:Now that we have the data and its in the right form the answer is just one line of code. Here are two ways:
The result of the last two lines is:
EDIT:
Regarding your question of doing it by week see the
nextfrifunction in the zoo quick reference vignette.