Skip to content
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

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

Closed
pachevalier opened this issue Oct 28, 2016 · 1 comment
Closed
Labels
bug an unexpected problem or unintended behavior

Comments

@pachevalier
Copy link

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)
@vspinu
Copy link
Member

vspinu commented Oct 28, 2016

This is a bug. Thanks.

But you should be using time_length instead:

int <- interval(start = ymd("2012-01-01"), end = ymd("2013-01-01"))
time_length(int, "year")
time_length(int, "month")

@vspinu vspinu added the bug an unexpected problem or unintended behavior label Oct 28, 2016
@vspinu vspinu closed this as completed in 697e01e Mar 3, 2017
vspinu added a commit that referenced this issue Mar 3, 2017
[Fix #490] Correctly compute length of period in months
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants