[R] Different way of aggregating

Phil Spector spector at stat.berkeley.edu
Thu Aug 19 01:55:20 CEST 2010


Skan -
    I'll answer this question:

"how can I calculate the mean of the first hour of all days, the mean of
the second hour of all days, and so on."

Since you didn't include a reproducible example, I have to make
some assumptions.  Since you are interested in hours, I'm guessing
your dates are stored as either cron objects or POSIXct objects.
Let's say your date is stored as a variable "date", and the variable
you wish to aggregate is stored as a numeric variable "value", both
in a data frame called "thedata"

For chron objects:

  aggregate(thedata$value,list(hour=hours(thedata$date)),mean)

For POSIXct objects:

  aggregate(thedata$value,list(hour=format(thedata$date,'%H')),mean)

If I made the wrong assumptions, please provide a reproducible example.

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu

On Wed, 18 Aug 2010, skan wrote:

>
> Hi
>
>
> Usually "aggregate" is used to calculate things such as the sum of all data
> on the first day, the sum next day, and so on.
> But how can I calculate the mean of the first hour of all days, the mean of
> the second hour of all days, and so on.  ???
>
> That's
> Most examples:
> today at 1am + today at 2am + today at 3am +....                    -> sum
> today
> tomorrow at 1am + tomorrow at 2am + tomorrow at 3am +....     -> sum
> tomorrow
> ..................
> aggregate(data, as.Date,sum)
>
>
> But what I need is:
> data today at 1 + data tomorrow at 1 + data in two days at 1 + .....    ->
> sum at 1am
> data today at 2 + data tomorrow at 2 + data in two days at 2 + .....    ->
> sum at 2am
> data today at 3 + data tomorrow at 3 + data in two days at 3 + .....    ->
> sum at 3am
> .............
>
> How can I do it??
>
> cheers
> -- 
> View this message in context: http://r.789695.n4.nabble.com/Different-way-of-aggregating-tp2330497p2330497.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
>



More information about the R-help mailing list