Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:09:04+00:00 2026-06-07T05:09:04+00:00

Just a little background: I got into programming through statistics, and I don’t have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:09:06+00:00Added an answer on June 7, 2026 at 5:09 am

    Read in the data, convert the DateAdded column to Date class, add a yearmon (year/month) column and then use sqldf or aggregate to aggregate by year/month:

    Lines <- "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"
    
    DF <- read.table(text = Lines, header = TRUE)
    
    # convert DateAdded column to Date class
    DF$DateAdded <- as.Date(DF$DateAdded, format = "%m/%d/%y")
    
    # add a year/month column using zoo
    library(zoo)
    DF$yearmon <- as.yearmon(DF$DateAdded)
    

    Now that we have the data and its in the right form the answer is just one line of code. Here are two ways:

    # 1. using sqldf
    library(sqldf)
    sqldf("select yearmon, avg(A), avg(B) from DF group by yearmon")
    
    # 2. using aggregate
    aggregate(cbind(A, B) ~ yearmon, DF, mean)
    

    The result of the last two lines is:

    > sqldf("select yearmon, avg(A), avg(B) from DF group by yearmon")
       yearmon avg(A) avg(B)
    1 Jan 2012   81.0   4303
    2 Feb 2012   32.5   3215
    > 
    > # 2. using aggregate
    > aggregate(cbind(A, B) ~ yearmon, DF, mean)
       yearmon    A    B
    1 Jan 2012 81.0 4303
    2 Feb 2012 32.5 3215
    

    EDIT:

    Regarding your question of doing it by week see the nextfri function in the zoo quick reference vignette.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just to give a little background: I'm using Dreamweaver CS5 for coding php, XAMPP
Just a little confused here... I have a function in postgres, and when I'm
Just a little background on my project: I'm implementing an SMS encryption program using
For a little background information, I've got an application that's running in a loop,
I'm just getting started with jQuery, having a little trouble. Got a table of
Just a little background: I am using Access 2010 to create forms and VBA
Little background: I have a JTextField called reading. It is located on a JFrame
Just a little background, our marketing department has been using static weather images on
Just a little background: I'm making a program where a user inputs a skeleton
Just a little edit, in case it helps anyone out who happens to stumble

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.