Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Problem to add a year when date is a 29 Feb #284

Closed
larmarange opened this issue Dec 12, 2014 · 8 comments
Closed

Problem to add a year when date is a 29 Feb #284

larmarange opened this issue Dec 12, 2014 · 8 comments

Comments

@larmarange
Copy link
Contributor

as.period(new_interval(ymd('1992-02-29'),ymd('2013-12-05')))

is working well. But

as.period(new_interval(ymd('1992-02-29'),ymd('2013-12-05')), unit="year")

creates the following error:

Error in while (any(start + est * per < end)) est[start + est * per <  : 
  missing value where TRUE/FALSE needed

According to RStudio debugger, the problem comes from line 49 in ops-division.r

@vspinu
Copy link
Member

vspinu commented Dec 12, 2014

What version is this? I cannot reproduce with the Dev version.
On Dec 12, 2014 2:48 AM, "Joseph" notifications@github.com wrote:

as.period(new_interval(ymd('1992-02-29'),ymd('2013-12-05')))

is working well. But

as.period(new_interval(ymd('1992-02-29'),ymd('2013-12-05')), unit="year)

creates the following error:

Error in while (any(start + est * per < end)) est[start + est * per < :
missing value where TRUE/FALSE needed

According to RStudio debugger, the problem comes from line 49 in
ops-division.r


Reply to this email directly or view it on GitHub
#284.

@larmarange
Copy link
Contributor Author

I have update R to version 3.1.2 and reinstalled the dev version with install_github from devtools. I'm on Windows 8.1, 64 bits, French version.

I still obtain:

> as.period(new_interval(ymd('1992-02-29'),ymd('2010-12-05')))
[1] "18y 9m 6d 0H 0M 0S"
> as.period(new_interval(ymd('1992-02-29'),ymd('2010-12-05')), unit="year")
Error in while (any(start + est * per < end)) est[start + est * per <  : 
  missing value where TRUE/FALSE needed
> as.period(new_interval(ymd('1992-02-29'),ymd('2010-12-05')), unit="month")
[1] "225m 6d 0H 0M 0S"

The bug occurs only if the first date is a 29 Feb and if unit="year"

@larmarange
Copy link
Contributor Author

Ok, the problem is due to the fact that in this specific case, on line 30 of ops-division.r (function adjust), we are doing this sort of computation:

ymd('1992-2-29')+period(1,"year")

whose result is equal to NA.

@larmarange
Copy link
Contributor Author

In the same way, we have:

> x <- ymd('1992-2-29')
> year(x)
[1] 1992
> year(x)<-1993
> x
[1] NA

I have changed the title of the issue accordingly

@larmarange larmarange changed the title Strange bug with as period Problem to add a year when date is a 29 Feb Dec 12, 2014
@larmarange
Copy link
Contributor Author

The issue is the same when adding months if the targeted month doesn't have the "same day" available. Example:

> ymd('2012-03-31')+period(1,"months")
[1] NA

@vspinu
Copy link
Member

vspinu commented Dec 12, 2014

The bug occurs only if the first date is a 29 Feb and if unit="year"

Ok. I see. This is why I haven't spotted it the first time. Will investigate.

@motabe
Copy link

motabe commented Apr 26, 2018

Guys, this is still happening nowadays, is there a way to cirvumvent this problem?

@cderv
Copy link
Contributor

cderv commented Apr 27, 2018

What are you trying to do exactly ?

initial problem is ok

library(lubridate)
as.period(interval(ymd('1992-02-29'),ymd('2013-12-05')), units = "year")
#> [1] "21y 9m 6d 0H 0M 0S"
as.period(interval(ymd('1992-02-29'),ymd('2013-12-05')))
#> [1] "21y 9m 6d 0H 0M 0S"

and about adding months to some date, this is correctly working and explained in Arithmetic with date times.

# 31 of April does not exists, thus NA
ymd('2012-03-31') + months(1)
#> [1] NA
#use %m+% to have the last day of the new month
ymd('2012-03-31') %m+% months(1)
#> [1] "2012-04-30"
ymd('2012-02-29') %m+% months(1)
#> [1] "2012-03-29"
# same with years
ymd('2012-03-31') + years(1)
#> [1] "2013-03-31"
ymd('2012-02-29') %m+% years(1)
#> [1] "2013-02-28"

Created on 2018-04-27 by the reprex package (v0.2.0).

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants