[R] Extracting specific rows from irregular zoo object and merging with a regular zoo object

Gabor Grothendieck ggrothendieck at gmail.com
Thu Apr 8 18:07:54 CEST 2010


On Thu, Apr 8, 2010 at 11:42 AM, Sergey Goriatchev <sergeyg at gmail.com> wrote:
> Thank you, Gabor! This is a very elegant solution.
> But instead of general last day of month in the index, how can I have
> last day of each month as they are presented in "a", for example, not
> March 31, but March 27?
>

Try this:

> a[!duplicated(as.yearmon(time(a)), fromLast = TRUE)]
2009-03-27 2009-04-30 2009-05-29 2009-06-26
        2          5          8          9

or this:

> last.date <- ave(time(a), as.yearmon(time(a)), FUN = function(x) tail(x, 1))
> aggregate(a, last.date, tail, 1)
2009-03-27 2009-04-30 2009-05-29 2009-06-26
        2          5          8          9

Note that the examples in ?aggregate.zoo include some that are very
similar examples are shown here.

Also see ?ave .



More information about the R-help mailing list