[R] aggregate.zoo

Gabor Grothendieck ggrothendieck at gmail.com
Thu May 13 23:27:25 CEST 2010


Here is a second solution where dly and year are as in the original
post.  The function returns the minimum index and applying that to the
sequence numbers of the days in each year we get a vector of the
indexes of the yearly minimums and then can use that to subscript dly:

f <- function(ix) ix[which.min(dly[ix])]
ix <- tapply(seq_along(dly), year, f)
dly[c(ix)]


On Thu, May 13, 2010 at 3:29 PM, Achim Zeileis <Achim.Zeileis at uibk.ac.at> wrote:
> On Thu, 13 May 2010, Hutchinson,David [PYR] wrote:
>
>> I have been using aggregate function to determine the annual minimum in
>> a daily time series. However, I was also hoping to determine the date of
>> occurrence without having to loop through each year to identify the date
>> of each minimum returned from aggregate(). Is there a more convenient
>> way of doing so?
>
> You could do something like this:
>
> ## determine position of minimum and number of observations
> ix <- aggregate(dly, year, function(x) c(which.min(x), length(x)))
>
> ## expand to overall index
> ix <- as.vector(cumsum(c(0, head(ix[,2], -1))) + ix[,1])
>
> ## subset original series
> dly[ix]
>
> hth,
> Z
>
>> Thanks in advance,
>>
>> Dave
>>
>> library(zoo)
>> n <- 1000
>> dly <- zoo(  x = rnorm(n),   order.by = seq.Date(as.Date('1970-01-01'),
>> by = 'day', length.out = n)   )
>> year <- as.numeric( format(index(dly), '%Y') )
>> annual.min <- aggregate( dly, by = year, FUN = min )
>>
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>
> ______________________________________________
> 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