[R] How to create sequence in month

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jul 12 21:56:27 CEST 2010


On Mon, Jul 12, 2010 at 2:25 PM, Bogaso Christofer
<bogaso.christofer at gmail.com> wrote:
> Hi all, can anyone please guide me how to create a sequence of months? Here
> I have tried following however couldn't get success
>
>> library(zoo)
>
>> seq(as.yearmon("2010-01-01"), as.yearmon("2010-03-01"), by="1 month")
>

There currently is no seq method (we will make a note to add one) for
yearmon but you can add the appropriate sequence to the starting
yearmon object, use zooreg or convert to numeric or Date, perform the
seq and then convert back

# yearmon + seq
as.yearmon("2010-01-01") + 0:2/12
as.yearmon("2010-01") + 0:2/12

# zooreg
time(zooreg(1:3, as.yearmon("2010-01"), freq = 12))

# seq.default
as.yearmon(seq(as.numeric(as.yearmon("2010-01")),
as.numeric(as.yearmon("2010-03")), 1/12))

# seq.Date
as.yearmon(seq(as.Date("2010-01-01"), as.Date("2010-03-01"), by = "month"))

Also note that if the reason you are doing this is to create a zooreg
object then its not necessary to explicitly form the sequence in the
first place since zooreg only requires the starting time and a
frequency as shown above.



More information about the R-help mailing list