[R] Summarizing data based on Date

John Kane jrkrideau at inbox.com
Wed Jun 10 15:04:32 CEST 2015


Hi Shivi

I think the names issue is just that that is aggregate()'s default. Just rename using ?names

For the 'sort' problem, AFAIK you cannot sort within aggregate(), at least not how you are doing it, nor do you want to do a sort().  You need ?order for what you want to do with a data.frame.

Sort is for vectors 

Does this do what you want?

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)

dat2  <-  aggregate(dat1$wt, list(dat1$dd), sum)

names(dat2)  <-  c("dd", "wt")

dat2 [order(dat2$dd),]

John Kane
Kingston ON Canada


> -----Original Message-----
> From: shivibhatia at ymail.com
> Sent: Tue, 9 Jun 2015 22:51:47 -0700 (PDT)
> To: r-help at r-project.org
> Subject: Re: [R] Summarizing data based on Date
> 
> HI All,
> 
> I am able to get the desired result. Thanks for extending help.
> while reading the csv file I made some changes as :
> 
> Test<-read.csv("Testdata.csv", head=TRUE, stringsAsFactors = FALSE,
> strip.white = TRUE)
> with this character var were not changed to factors.
> 
> Then aggregation was simple:
> aggregate(test$CHG_WT, list(test$CR_DT), sum)
> 
> However the output is not sorted based on Dates and the columns names
> appearing as very different:
> 
> Group.1       x
> 1   1-Mar-15  909791
> 2  10-Mar-15  822436
> 3  11-Mar-15  848609
> 4  12-Mar-15  924842
> 5  13-Mar-15  895270
> 6  14-Mar-15  93238
> 7 2-Mar-15     731600
> 
> Can you all please suggest why the column names are so different and how
> I
> could sort based on dates. I added the sort option in the above syntax
> aggregate(test$CHG_WT, list(test$CR_DT), sum,sort(test$CR_DT,decreasing =
> TRUE))
> 
> But it gave me an error:
> Error in FUN(X[[i]], ...) : invalid 'type' (character) of argument
> Thanks All.
> 
> 
> 
> 
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Summarizing-data-based-on-Date-tp4708328p4708423.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.

____________________________________________________________
Can't remember your password? Do you need a strong and secure password?
Use Password manager! It stores your passwords & protects your account.



More information about the R-help mailing list