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 8477291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:25:27+00:00 2026-06-10T18:25:27+00:00

I consistently need to take transaction data and aggregate it by Day, Week, Month,

  • 0

I consistently need to take transaction data and aggregate it by Day, Week, Month, Quarter, Year – essentially, it’s time-series data. I started to apply zoo/xts to my data in hopes I could aggregate the data faster, but I either don’t fully understand the packages’ purpose or I’m trying to apply it incorrectly.

In general, I would like to calculate the number of orders and the number of products ordered by category, by time period (day, week, month, etc).

#Create the data
clients <- 1:10
dates <- seq(as.Date("2012/1/1"), as.Date("2012/9/1"), "days")
categories <- LETTERS[1:5]
products <- data.frame(numProducts = 1:10, 
                       category = sample(categories, 1000, replace = TRUE),
                       clientID = sample(clients, 1000, replace = TRUE), 
                       OrderDate = sample(dates, 1000, replace = TRUE))

I could do this with plyr and reshape, but I think this is a round-about way to do so.

#Aggregate by date and category
products.day <- ddply(products, .(OrderDate, category), summarize, numOrders = length(numProducts), numProducts = sum(numProducts))

#Aggregate by Month and category
products.month <- ddply(products, .(Month = months(OrderDate), Category = category), summarize, numOrders = length(numProducts), numProducts = sum(numProducts))

#Make a wide-version of the data frame
products.month.wide <- cast(products.month, Month~Category, sum)

I tried to apply zoo to the data like so:

products.TS <- aggregate(products$numProducts, yearmon, mean) 

It returned this error:

Error in aggregate.data.frame(as.data.frame(x), ...) : 
  'by' must be a list

I’ve read the zoo vignettes and documentation, but every example that I’ve found only shows 1 record/row/entry per time entry.

Do I have to pre-aggregate the data I want to time-series on? I was hoping that I could simply group by the fields I want, then have the months or quarters get added to the data frame incrementally to the X-axis.

Is there a better approach to aggregating this or a more appropriate package?

  • 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-10T18:25:29+00:00Added an answer on June 10, 2026 at 6:25 pm

    products$numProducts is a vector, not a zoo object. You’d need to create a zoo object before you can use method dispatch to call aggregate.zoo.

    pz <- with(products, zoo(numProducts, OrderDate))
    products.TS <- aggregate(pz, as.yearmon, mean)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that looks like this. I need to consistently write something
I'm writing an application and need some help with consistently storing and loading preferences
Have few thousand reports that have consistently formatted tabular data embedded within them that
I need to plot the transformation of a 3d object against time. I have
I need a basic, how-to guide on how to take a picture from MY
I consistently run into the need to clean up css files and strip unused
I need a consistent way to monitor databases that are read-only log shipped copies
I need a safe (i.e consistent, robust) way of detecting whether or not the
I've consistently had an issue with parsing XML with PHP and not really found
I have consistently had IDE problems in Delphi/C++Builder for years, with every version. I

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.