Skip to content

Weird results trying to compute the lengths of a period in months  #490

@pachevalier

Description

@pachevalier

I've got weird results when I try to convert an interval as a period and compute the length of the period in months.

I use lubridate and magrittr

> library(lubridate)
> library(magrittr)

I take as interval the time period between 2012, Jan 1st and 2013, Jan 1st.

When I try to compute the length of the period in days. I've got expected value 325.25 :

> interval(start = ymd("2012-01-01"), end = ymd("2013-01-01")) %>% as.period() %>% as.numeric("days")
[1] 365.25

When I try to compute the length of the period in years, I've got the expected value 1 :

> interval(start = ymd("2012-01-01"), end = ymd("2013-01-01")) %>% as.period() %>% as.numeric("years")
 [1] 1

But when I try to compute the length in months, I've got the inverse of the expected value :

> interval(start = ymd("2012-01-01"), end = ymd("2013-01-01")) %>% as.period() %>% as.numeric("months")
 [1] 0.08333333

Note that

> 1/0.08333333
[1] 12

So the outcome is one over the expected value.

Is it a bug ? Am I doing something wrong in my code ?

> library(devtools)
> session_info()
Session info -------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.1 (2016-06-21)
 system   x86_64, linux-gnu           
 ui       X11                         
 language (EN)                        
 collate  fr_FR.utf8                  
 tz       Europe/Paris                
 date     2016-10-28                  

Packages -----------------------------------------------------------------------
 package   * version date       source        
 devtools  * 1.12.0  2016-06-24 CRAN (R 3.3.1)
 digest      0.6.10  2016-08-02 CRAN (R 3.3.1)
 lubridate * 1.6.0   2016-09-13 CRAN (R 3.3.1)
 magrittr  * 1.5     2014-11-22 CRAN (R 3.3.0)
 memoise     1.0.0   2016-01-29 CRAN (R 3.3.0)
 stringi     1.1.1   2016-05-27 CRAN (R 3.3.0)
 stringr     1.1.0   2016-08-19 CRAN (R 3.3.1)
 withr       1.0.2   2016-06-20 CRAN (R 3.3.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions