[R] End of Month date capture
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Nov 11 16:36:46 CET 2009
Here is one possibility using aggregate.zoo
> aggregate(z, as.yearmon, tail, 1)
x1 x2 x3 x4 x5 x6
Aug 1998 -0.07043759 NA NA NA NA NA
Sep 1998 -0.03098945 NA NA NA NA NA
Oct 1998 -0.00726802 NA NA NA NA NA
The above is not strictly what you asked for since it uses yearmon
times. Here is another solution in which we create a new zoo object
of sequential indexes and then extract the aggregated indexes from z.
This also uses aggregate.zoo:
> tt <- time(z)
> ix <- coredata(aggregate(zoo(seq_along(tt), tt), as.yearmon, tail, 1))
> z[ix, ]
x1 x2 x3 x4 x5 x6
1998-08-31 -0.07043759 NA NA NA NA NA
1998-09-30 -0.03098945 NA NA NA NA NA
1998-10-23 -0.00726802 NA NA NA NA NA
Here is one that uses aggregate from the core of R rather than aggregate.zoo:
> tt <- time(z)
> ix <- aggregate(seq_along(tt), list(as.yearmon(tt)), tail, 1)$x
> z[ix, ]
x1 x2 x3 x4 x5 x6
1998-08-31 -0.07043759 NA NA NA NA NA
1998-09-30 -0.03098945 NA NA NA NA NA
1998-10-23 -0.00726802 NA NA NA NA NA
On Wed, Nov 11, 2009 at 10:06 AM, Research <risk2009 at ath.forthnet.gr> wrote:
> Dear R-users,
>
> I have the following zoo object:
>
> x1 x2 x3 x4 x5
> x6
> 1998-08-31 -0.0704375904 NA NA NA NA
> NA
> 1998-09-01 0.0379028122 NA NA NA NA
> 0.00609139
> 1998-09-02 -0.0038191639 NA NA NA NA
> NA
> 1998-09-03 -0.0083235389 NA NA NA NA
> NA
> 1998-09-04 -0.0085576782 NA NA 0.0028570541 NA
> NA
> 1998-09-07 0.0000000000 NA NA NA NA
> NA
> 1998-09-08 0.0496459618 NA NA NA NA
> NA
> 1998-09-09 -0.0170081847 NA NA NA NA
> NA
> 1998-09-10 -0.0261897076 NA NA NA NA
> NA
> 1998-09-11 0.0290280530 NA NA NA NA
> NA
> 1998-09-14 0.0202677162 NA NA NA NA
> NA
> 1998-09-15 0.0077005314 NA NA NA NA
> NA
> 1998-09-16 0.0074886581 NA NA NA -0.002710978
> NA
> 1998-09-17 -0.0257819401 NA NA NA NA
> NA
> 1998-09-18 0.0011966887 NA NA NA NA
> NA
> 1998-09-21 0.0037182403 NA NA NA NA
> NA
> 1998-09-22 0.0055904154 NA NA NA NA
> NA
> 1998-09-23 0.0347982355 NA NA NA NA
> NA
> 1998-09-24 -0.0221650663 NA NA NA NA
> NA
> 1998-09-25 0.0019449387 NA 0.007833611 NA NA
> NA
> 1998-09-28 0.0037641439 NA NA NA NA
> NA
> 1998-09-29 0.0003146288 NA NA NA NA
> NA
> 1998-09-30 -0.0309894451 NA NA NA NA
> NA
> 1998-10-01 -0.0305704149 NA NA NA NA
> 0.00000000
> 1998-10-02 0.0163000909 -0.03409975 NA 0.0004991463 NA
> NA
> 1998-10-05 -0.0141025660 NA NA NA NA
> NA
> 1998-10-06 -0.0040240279 NA NA NA NA
> NA
> 1998-10-07 -0.0142284540 NA NA NA NA
> NA
> 1998-10-08 -0.0116470759 NA NA NA NA
> NA
> 1998-10-09 0.0256723791 NA NA NA NA
> NA
> 1998-10-12 0.0134404929 NA NA NA NA
> NA
> 1998-10-13 -0.0029209410 NA NA NA NA
> NA
> 1998-10-14 0.0107283327 NA NA NA NA
> NA
> 1998-10-15 0.0408820605 NA NA NA NA
> NA
> 1998-10-16 0.0084890073 NA NA NA 0.006675831
> NA
> 1998-10-19 0.0056352536 NA NA NA NA
> NA
> 1998-10-20 0.0014485122 NA NA NA NA
> NA
> 1998-10-21 0.0056142800 NA NA NA NA
> NA
> 1998-10-22 0.0079687631 NA NA NA NA
> NA
> 1998-10-23 -0.0072680217 NA NA NA NA
> NA
>>
>
>
> I need to create second one which contains the end of each month rows only.
> Is there an easy way of doing this? The data starts at 1998-08-31 and ends
> at 2009-11-06.
>
> Many thanks in advance,
> Costas
More information about the R-help
mailing list