Re: [R] adding 1 month to a date

From: bogdan romocea <br44114_at_gmail.com>
Date: Wed 12 Oct 2005 - 22:13:17 EST


Simple addition and subtraction works as well:   as.Date("1995/12/01",format="%Y/%m/%d") + 30 If you have datetime values you can use
  strptime("1995-12-01 08:00:00",format="%Y-%m-%d %H:%M:%S") + 30*24*3600
where 30*24*3600 = 30 days expressed in seconds.

> -----Original Message-----
> From: Marc Schwartz [mailto:MSchwartz@mn.rr.com]
> Sent: Tuesday, October 11, 2005 10:16 PM
> To: t c
> Cc: r-help@stat.math.ethz.ch
> Subject: Re: [R] adding 1 month to a date
>
>
> On Tue, 2005-10-11 at 16:26 -0700, t c wrote:
> > Within an R dataset, I have a date field called date_.
> (The dates are
> > in the format YYYY-MM-DD, e.g. 1995-12-01.)
>
> > How can I add or subtract 1 month from this date, to get
> 1996-01-01 or
> > 1995-11-01.
>
> There might be an easier way to do this, but using seq.Date(), you can
> increment or decrement from a Time 0 by months:
>
> Add 1 month:
>
> This takes your Time 0, generates a 2 element sequence (which begins
> with Time 0) and then takes the second element:
>
> > seq(as.Date("1995-12-01"), by = "month", length = 2)[2]
> [1] "1996-01-01"
>
>
>
> Subtract 1 month:
>
> Same as above, but we use 'by = "-1 month"' and take the
> second element:
>
> > seq(as.Date("1995-12-01"), by = "-1 month", length = 2)[2]
> [1] "1995-11-01"
>
>
> See ?as.Date and ?seq.Date for more information. The former
> function is
> used to convert from a character vector to a Date class object. Note
> that in your case, the date format is consistent with the default. Pay
> attention to the 'format' argument in as.Date() if your dates
> should be
> in other formats.
>
> HTH,
>
> Marc Schwartz
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Oct 12 22:20:15 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:41 EST