Skip to content

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

Closed

Description

@larmarange
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

Activity

  1. vspinu commented on Dec 12, 2014

    @vspinu
    Member

    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.

  2. larmarange commented on Dec 12, 2014

    @larmarange
    ContributorAuthor

    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"

  3. larmarange commented on Dec 12, 2014

    @larmarange
    ContributorAuthor

    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.

  4. larmarange commented on Dec 12, 2014

    @larmarange
    ContributorAuthor

    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

  5. changed the title [-]Strange bug with as period[/-] [+]Problem to add a year when date is a 29 Feb[/+] on Dec 12, 2014
  6. larmarange commented on Dec 12, 2014

    @larmarange
    ContributorAuthor

    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
  7. vspinu commented on Dec 12, 2014

    @vspinu
    Member

    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.

  8. motabe commented on Apr 26, 2018

    @motabe

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

  9. cderv commented on Apr 27, 2018

    @cderv
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions