[R] aggregating using a non-summary function?

Gabor Grothendieck ggrothendieck at gmail.com
Sat Aug 22 17:08:35 CEST 2009


On Wed, Aug 19, 2009 at 12:14 AM, Gabor
Grothendieck<ggrothendieck at gmail.com> wrote:
> On Tue, Aug 18, 2009 at 7:09 PM, Sherri Heck<sheck at ucar.edu> wrote:
>> Dear all-
>>
>> I have a data set that looks as follows (data are taken every 5 minutes):
>>
>>      LST   in     mph    Deg   DegF  DegF2    %    volts   Deg    mph2   w/m2
>> 0905010000   -999    7.5  259.3   33.0   -999   41.5   -999   -999    9.1    0.2
>> 0905010005   -999   11.0  261.2   33.1   -999   42.6   -999   -999   18.2    0.2
>> 0905010010   -999    8.9  252.5   33.4   -999   41.6   -999   -999   12.4    0.2
>> 0905010015   -999    9.1  265.9   33.2   -999   41.8   -999   -999   11.7    0.2
>>
>>
>> I am able to calculate the hourly average of "mph" over a span of 3 months with the following code:
>>
>>
>> z <- read.zoo("SPL summer 2009.txt", header = TRUE, na.strings = -999,
>> format = "%y%m%d%H%M", FUN = as.chron,
>> colClasses = c("character", rep("numeric", 10)))
>> mph <- z[months(time(z)) %in% c("May", "Jun", "Jul"),]
>> ww <- aggregate(z$mph, trunc(time(z), "hour"), mean)
>>
>>
>> but, i need to take the hourly average (spanning the same three months) of the directional degrees (Deg) in order to use that data to create a wind rose.  I was planning to get the hourly avg step by step , i.e.  converting to radians, taking sins, etc, but "aggregate" seems to only use summary-type functions.  I have been playing around with "daply", which seems to accept other functions, but am not sure how to convert the time/date stamp within it.
>>
>
> You can aggregate by any function that inputs a vector and produces
> a scalar.  Can you clarify what function you want to aggregate by that you
> believe cannot be handled?
>

Just to finish off this thread what turned out to be wanted was this
slight variation of the original code:

aggregate(sin(z$Deg * pi / 180), trunc(time(z), "hour"), mean)




More information about the R-help mailing list