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

Sergey Goriatchev sergeyg at gmail.com
Thu Apr 8 17:42:47 CEST 2010


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?

Regards,
Sergey

On Thu, Apr 8, 2010 at 17:27, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Omit rep.  You just want  a <- zoo(1:9, ...). To get the last day of
> the month you don`t need b since as.Date.yearmon will give it with the
> argument frac = 1:
>
>> aggregate(a, as.Date(as.yearmon(time(a)), frac = 1), tail, 1)
> 2009-03-31 2009-04-30 2009-05-31 2009-06-30
>         2          5          8          9
>
>
> On Thu, Apr 8, 2010 at 11:18 AM, Sergey Goriatchev <sergeyg at gmail.com> wrote:
>> Hello, everyone
>>
>> I have the following problem:
>> Say I have an irregular zoo timeseries like this:
>>
>> a <- zoo(rep(1:9), as.Date(c("2009-03-20", "2009-03-27", "2009-04-24",
>> "2009-04-25", "2009-04-30", "2009-05-15", "2009-05-22", "2009-05-29",
>> "2009-06-26")))
>>
>> and I have regular zoo timeseries like this:
>>
>> b <- zoo(rep(1:4), as.Date(c("2009-03-31", "2009-04-30", "2009-05-31",
>> "2009-06-30")))
>>
>> >From "a" I need to extract those rows that hold values for the last
>> day of each month (creating series "c"). Then I have to merge these
>> values with "b", such that the result has the index of "c".
>>
>> How could I do this most efficiently?
>>
>> Thank you in advance!
>>
>> Best,
>> Sergey
>>
>> --
>> Simplicity is the last step of art./Bruce Lee
>>
>> ______________________________________________
>> 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.
>>
>



-- 
Simplicity is the last step of art./Bruce Lee
The more you know, the more you know you don't know. /Myself

I'm not young enough to know everything. /Oscar Wilde
Experience is one thing you can't get for nothing. /Oscar Wilde
When you are finished changing, you're finished. /Benjamin Franklin
Luck is where preparation meets opportunity. /George Patten

Kniven skärpes bara mot stenen.



More information about the R-help mailing list