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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:13:40+00:00 2026-06-07T04:13:40+00:00

The aggregate function giving me mean sales by month works fine. library(chron) set.seed(42) dat

  • 0

The aggregate function giving me mean sales by month works fine.

library(chron)
set.seed(42)
dat <- data.frame(sales = rnorm(1000, mean = 1000, sd = 40),
              dates = rep(as.Date(seq(from = 14610, to = 14859),
                              origin = "1970-01-01"),4))
aggregate(sales~months(as.chron(dates)), mean, data=dat)

…and produces the following output:

months(as.chron(dates))     sales
1                     Jan 1000.0723
2                     Feb  999.1580
3                     Mar  995.3055
4                     Apr 1000.4912
5                     May 1003.9703
6                     Jun  997.1086
7                     Jul  996.5939
8                     Aug  998.5012
9                     Sep 1001.3709

My understanding is that the following cast statement should produce the same output:

cast(dat, months(as.chron(dates)) ~ ., mean, value="sales")

but is instead returning the following error:

Error: Casting formula contains variables not found in molten data: months(as.chron(dates))

I’m likely missing something but is it possible to use the chron months() call inside of a cast statement? The following two statements will accomplish the same in cast() but I’m trying to do it in one step and better understand how cast works.

dat$mont <- months(as.chron(dat$dates))
cast(dat, mont ~ ., mean, value="sales")

Thanks in advance,
–JT

  • 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-07T04:13:41+00:00Added an answer on June 7, 2026 at 4:13 am

    This will work with reshape2

    library(reshape2)
    dcast(dat, months(as.chron(dates)) ~ ., mean, value.var="sales")
    ##   months(as.chron(dates))        NA
    ## 1                     Jan 1004.5404
    ## 2                     Feb 1002.3146
    ## 3                     Mar  996.0883
    ## 4                     Apr  994.1707
    ## 5                     May 1000.4652
    ## 6                     Jun 1002.8020
    ## 7                     Jul  996.0357
    ## 8                     Aug 1001.6754
    ## 9                     Sep  997.6772
    

    or you could use plyr

    library(plyr)
    ddply(dat, .(months = months(as.chron(dates))), summarize, sales = mean(sales))
    ##  months     sales
    ## 1   Jan 1004.5404
    ## 2   Feb 1002.3146
    ## 3   Mar  996.0883
    ## 4   Apr  994.1707
    ## 5   May 1000.4652
    ## 6   Jun 1002.8020
    ## 7   Jul  996.0357
    ## 8   Aug 1001.6754
    ## 9   Sep  997.6772
    

    or with data.table

    library(data.table)
    DT <- data.table(dat)
    DT[, month := months(as.chron(dates))][,list(sales =  mean(sales)),by = month]
    ##    month     sales
    ## 1:   Jan 1004.5404
    ## 2:   Feb 1002.3146
    ## 3:   Mar  996.0883
    ## 4:   Apr  994.1707
    ## 5:   May 1000.4652
    ## 6:   Jun 1002.8020
    ## 7:   Jul  996.0357
    ## 8:   Aug 1001.6754
    ## 9:   Sep  997.6772
    

    Comment from Matthew Dowle

    The := isn’t needed, iiuc, as by accepts expressions directly :

    DT[, list(sales=mean(sales)), by=months(as.chron(dates))]
    ##    months     sales
    ## 1:    Jan 1004.5404
    ## 2:    Feb 1002.3146
    ## 3:    Mar  996.0883
    ## 4:    Apr  994.1707
    ## 5:    May 1000.4652
    ## 6:    Jun 1002.8020
    ## 7:    Jul  996.0357
    ## 8:    Aug 1001.6754
    ## 9:    Sep  997.6772
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

aggregate function works fine aggregate(weight ~ feed, data = chickwts, mean) feed weight 1
I have an user defined table function in SQL Server that aggregate data from
Excel has the KURT function that returns the kurtosis of a data set. Does
An SQL aggregate function counting number of name enteries in DB. string cnnStr =
Does DISTINCT in a simple query with an aggregate function have any effect? select
Clearly, when GROUP BY clause used, columns that are not aggregate function should be
I'm trying to aggregate a bunch of user profile data in our app. Each
How can we aggregate function (MIN, MAX, COUNT etc) in Entity framework 3.5 in
The standard SQL aggregate function max() will return the highest value in a group;
is it possible to alias an aggregate function in a select clause as AliasTable.AliasColumn?

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.