[R] Calculation with date

Dan Murphy chiefmurphy at gmail.com
Mon Mar 11 17:25:18 CET 2013


The lubridate and seq solutions work because they rely on your
specification that the returned date be the first day of the desired
subsequent months. Without that requirement they would not always
work. For example, if you wanted to add Vec months to the last day of
March rather than the first day, the second date produced by those
solutions is in May which the month following the desired month
(April):

> Date <- as.Date("2013-03-31")
> library(lubridate)
> as.Date(dmy(format(Date-8, "%d-%m-%Y"))+months(Vec))
 1 parsed with %d-%m-%Y
[1] "2013-03-31" "2014-05-01" "2014-01-31" "2013-07-31"

and

> seq(Date, length=diff(range(Vec))+1, by="month")[Vec+1]
[1] "2013-03-31" "2014-05-01" "2014-01-31" "2013-07-31"


The mondate package does date arithmetic in months and is guaranteed
to give you a date in your desired number of subsequent months (or
previous months, btw):

> library(mondate)
> as.Date(mondate(Date) + Vec)
[1] "2013-03-31" "2014-04-30" "2014-01-31" "2013-07-31"

To get the first day of the month, you could simply subtract the day
number of the result, as shown in previous replies:

> res <- as.Date(mondate(Date) + Vec)
> res - as.POSIXlt(res)$mday + 1
[1] "2013-03-01" "2014-04-01" "2014-01-01" "2013-07-01"

or you could do the calculation with mondate's as follows

> res <- mondate(Date) + Vec
> as.Date(mondate.ymd(year(res), month(res), 1))
[1] "2013-03-01" "2014-04-01" "2014-01-01" "2013-07-01"

Hope that helps,
Dan


==================

Message: 16
Date: Sat, 9 Mar 2013 08:37:23 -0800
From: David Winsemius <dwinsemius at comcast.net>
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: r-help <r-help at r-project.org>
Subject: Re: [R] Calculation with date
Message-ID: <01E8683D-60BE-4750-AB1B-6047162CC175 at comcast.net>
Content-Type: text/plain; charset=us-ascii


On Mar 9, 2013, at 4:24 AM, Rui Barradas wrote:

> Hello,
>
> I don't believe there's such a function, but you can write one.

I beg to disagree. The seq.Date function lets one create sequences by
month. The only added twist in this case is to subract to the
beginning of the current month:

> seq( Date- as.POSIXlt(Date)$mday+1, length=diff(range(Vec))+1, by="month")[Vec+1]
[1] "2013-03-01" "2014-04-01" "2014-01-01" "2013-07-01"


>
> Date <- as.Date(Sys.time())
> New_Vec <- c("2013-03-01", "2014-04-01", "2014-01-01", "2013-07-01")
> New_Vec <- as.Date(New_Vec)
> Vec <- c(0, 13, 10, 4)
>
>
> plusmonths <- function(x, y){
>       s <- as.integer(format(x, "%m")) + y
>       yx <- as.integer(format(x, "%Y")) + (s %/% 12)
>       as.Date(paste(yx, s %% 12, "01", sep = "-"))
> }
> pm <- plusmonths(Date, Vec)
>
> identical(New_Vec, pm)  # TRUE
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 09-03-2013 11:41, Christofer Bogaso escreveu:
>> Hello again,
>>
>> Let say I have an non-negative integer vector (which may be random):
>>
>> Vec <- c(0, 13, 10, 4)
>>
>> And I have a date:
>>
>>> Date <- as.Date(Sys.time())
>>> Date
>> [1] "2013-03-09"
>>
>>
>>
>> Using these 2 information, I want to get following date-vector:
>>
>> New_Vec <- c("2013-03-01", "2014-04-01", "2014-01-01", "2013-07-01")
>>
>> Basically the month-difference between 'New_Vec' and 'Date' will be 'Vec '
>>
>> Is there any R function to do it programmatically?
>>
>>
>> Thanks for your help.
>>
>> ______________________________________________
>> 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.
>>
>
> ______________________________________________
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list