[R] Summarizing data based on Date

John Kane jrkrideau at inbox.com
Tue Jun 9 18:42:05 CEST 2015


Hi,

As David said have a look at str(test). You have a factor in there or else that weird "list(format(test$CR_DT,"%m"))" command in aggregate() is mucking things up.  What is "list(format(test$CR_DT,"%m"))" intended to do?  No ,a quick test says it is mucking something else up and not giving the us the factor problem. 

Here is your sample data and what I think is what you are trying to do. Note the data is supplied in dput() format which is the preferred way to supply sample data to the R-help list.  See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and http://adv-r.had.co.nz/Reproducibility.html for more information.  I used lubridate's dmy() function rather than as.Date() to format the dates.

dat1  <-  structure(list(dd = structure(c(1426204800, 1427760000, 1426377600, 
1426550400, 1426550400, 1426032000, 1426032000, 1426723200), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), wt = c(0, 0, 0, 770, 3.73, 70, 10, 500)), .Names = c("dd", 
"wt"), row.names = c(NA, -8L), class = "data.frame")

str(dat1)

aggregate(dat1$wt, list(dat1$dd), sum)


John Kane
Kingston ON Canada


> -----Original Message-----
> From: shivibhatia at ymail.com
> Sent: Tue, 9 Jun 2015 05:01:23 -0700 (PDT)
> To: r-help at r-project.org
> Subject: Re: [R] Summarizing data based on Date
> 
> Hi Petr
> 
> I researched a lot over the net and R manual as well based on which I
> revamped my code and came to the code as:
> test$CR_DT <- as.Date(test$CR_DT, '%d-%b-%y')
> 
> iii<- aggregate(test$CHG_WT,list(format(test$CR_DT,"%m")),FUN=sum)
> 
> However it still gives me the error as below:
> Error in Summary.factor(c(1L, 1L, 1L, 3286L, 1646L, 3241L, 1L, 1L, 1307L,
> :
>   ‘sum’ not meaningful for factors.
> 
> If could you guide on how to achieve the desired output. Thanks.
> 
> 
> 
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Summarizing-data-based-on-Date-tp4708328p4708384.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if5
Capture screenshots, upload images, edit and send them to your friends
through IMs, post on Twitter®, Facebook®, MySpace™, LinkedIn® – FAST!



More information about the R-help mailing list