[R] adding 1 month to a date

David Forrest drf5n at maplepark.com
Wed Oct 12 18:50:22 CEST 2005


On Wed, 12 Oct 2005, Jim Porzak wrote:

>  OTOH,
>
>  > seq(as.Date("2004-01-31"), by = "month", length = 14)
>   [1] "2004-01-31" "2004-03-02" "2004-03-31" "2004-05-01" "2004-05-31"
>   [6] "2004-07-01" "2004-07-31" "2004-08-31" "2004-10-01" "2004-10-31"
>  [11] "2004-12-01" "2004-12-31" "2005-01-31" "2005-03-03"
>
>  I would prefer to see dates forced to be within each month, not
>  "leaking" into next month.
>
>  IOW:
>   [1] "2004-01-31" "2004-02-29" "2004-03-31" "2004-04-30" "2004-05-31", etc

It depends how you intend "1 month after 2004-01-31".  Is the the same
number of days before the beginning of the next month or after the end of
the stated month?

   seq(as.Date("2004-02-01"), by = "month", length = 14)-1

   [1] "2004-01-31" "2004-02-29" "2004-03-31" "2004-04-30" "2004-05-31"

Dave
-- 
 Dr. David Forrest
 drf at vims.edu                                    (804)684-7900w
 drf5n at maplepark.com                             (804)642-0662h
                                   http://maplepark.com/~drf5n/




More information about the R-help mailing list