[R] Finding a date 6 months before

Gabor Grothendieck ggrothendieck at gmail.com
Wed Mar 3 12:24:43 CET 2010


Try this.  as.Date(as.yearmon(today) - 6/12) gives the 1st of the
month 6 months ago and the remainder of that line moves ahead by d-1
days if its the d-th day of the month now.

> library(zoo)
> z <- zoo(0:399, as.Date("2009-06-01")+0:399)
> today <- Sys.Date(); today
[1] "2010-03-03"
> ago6mos <- as.Date(as.yearmon(today) - 6/12) - 1 + as.numeric(format(today, "%d"))
> window(z, index = ago6mos)
2009-09-03
        94

If we are at the last day of the month and 6 months ago that month has
fewer days than today's month then what to do is not well defined but
we resolve that here by just moving ahead d-1 days anyways.

On Wed, Mar 3, 2010 at 6:07 AM, Sergey Goriatchev <sergeyg at gmail.com> wrote:
> Hello everyone
>
> I use zoo objects and I need to find a date 6 months before today's date.
>
> Example, today we have 3rd March 2010, I need to find the date 3rd
> September 2009.
>
> How could I do that, please?
>
>
> Regards,
> Sergey
>
> --
> Simplicity is the last step of art./Bruce Lee
> The more you know, the more you know you don't know.
>
> ______________________________________________
> 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