New issue

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

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

Already on GitHub? Sign in 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

Comments

Projects
None yet
4 participants
@larmarange
Contributor

larmarange commented Dec 12, 2014

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

This comment has been minimized.

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

This comment has been minimized.

Contributor

larmarange commented Dec 12, 2014

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

This comment has been minimized.

Contributor

larmarange commented Dec 12, 2014

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

This comment has been minimized.

Contributor

larmarange commented Dec 12, 2014

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 from Strange bug with as period to Problem to add a year when date is a 29 Feb Dec 12, 2014

@larmarange

This comment has been minimized.

Contributor

larmarange commented Dec 12, 2014

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

This comment has been minimized.

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

This comment has been minimized.

motabe commented Apr 26, 2018

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

@cderv

This comment has been minimized.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment