[R] aggregate.zoo

Achim Zeileis Achim.Zeileis at uibk.ac.at
Thu May 13 21:29:26 CEST 2010


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.
>



More information about the R-help mailing list