seq.Date when date is the last date of the month

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

seq.Date when date is the last date of the month

Jeremie Juste
Hello,

I recently bumped into a behavior that surprised me.
When performing the following command, I would expect the second
argument to be "2012-09-30" but got "2012-10-01" instead
> seq(as.Date("2012-08-31"),by="1 month",length=2)
[1] "2012-08-31" "2012-10-01"

When the same command is performed for the start of the month. I get a
result I expect.
> seq(as.Date("2012-08-01"),by="1 month",length=2)
[1] "2012-08-01"


Is there an explanation for this behavior?

Best regards,
--
Jeremie Juste

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: seq.Date when date is the last date of the month

jholtman
yes it is the expected behaviour is you check the documentation:

Using "month" first advances the month without changing the day: if
this results in an invalid day of the month, it is counted forward
into the next month: see the examples.

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Thu, Jan 7, 2021 at 11:20 AM Jeremie Juste <[hidden email]> wrote:

>
> Hello,
>
> I recently bumped into a behavior that surprised me.
> When performing the following command, I would expect the second
> argument to be "2012-09-30" but got "2012-10-01" instead
> > seq(as.Date("2012-08-31"),by="1 month",length=2)
> [1] "2012-08-31" "2012-10-01"
>
> When the same command is performed for the start of the month. I get a
> result I expect.
> > seq(as.Date("2012-08-01"),by="1 month",length=2)
> [1] "2012-08-01"
>
>
> Is there an explanation for this behavior?
>
> Best regards,
> --
> Jeremie Juste
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: seq.Date when date is the last date of the month

Dirk Eddelbuettel
In reply to this post by Jeremie Juste

Jeremie,

As months have irregular number of dates, one needs to use a function that
accounts for that (date libraries and packages have that, one of the earliest
for R was my RcppBDT package using Boost Date_Time), or be otherwise clever.

Here is a one-liner using the latter approach:

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

See this old StackOverflow answer where I used this before:

   https://stackoverflow.com/questions/8333838/generate-a-sequence-of-the-last-day-of-the-month-over-two-years

Dirk

--
https://dirk.eddelbuettel.com | @eddelbuettel | [hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: seq.Date when date is the last date of the month

Jeremie Juste
In reply to this post by jholtman

Hello Jim,

Many thanks for the feedback

> Using "month" first advances the month without changing the day: if
> this results in an invalid day of the month, it is counted forward
> into the next month: see the examples.
Indeed I missed the documentation of seq.Date that refers to
seq.POSIXt. Many thanks for pointing this out.

Still I would be tempted to count back the day into the same month
instead of counting forward. But this behavior seems intentional and
documented so no need to question it.

> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
The problem I'm trying to solve is this.

for a date d and an integer m
add.month(d,m)

would

Using "month" first advance the month without changing the day: if
this results in an invalid day of the month, it is counted backward
into the same month.

so nearly the same behavior as seq.Date except
add.month("2020-08-31",1) ==> 2020-09-30
add.month("2020-08-01",1) ==> 2020-09-01


Best regards,
Jeremie

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: seq.Date when date is the last date of the month

Jeremie Juste
In reply to this post by Dirk Eddelbuettel
Hello Dirk,

Many thanks for the feedback. I did came across your SO answer but
didn't know the RcppBDT package, thanks for pointing that out.

Best regards,
Jeremie

On Thursday,  7 Jan 2021 at 13:59, Dirk Eddelbuettel wrote:

> Jeremie,
>
> As months have irregular number of dates, one needs to use a function that
> accounts for that (date libraries and packages have that, one of the earliest
> for R was my RcppBDT package using Boost Date_Time), or be otherwise clever.
>
> Here is a one-liner using the latter approach:
>
>    seq(as.Date("2010-02-01"), length=24, by="1 month") - 1
>
> See this old StackOverflow answer where I used this before:
>
>    https://stackoverflow.com/questions/8333838/generate-a-sequence-of-the-last-day-of-the-month-over-two-years
>
> Dirk

--
Jeremie Juste

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.