11

Consider following:

library(lubridate)
period1 = weeks(2)
as.numeric(period1, "weeks")
> 2   # as expected 

Now I am trying to do similar with months:

period2= months(6)
as.numeric(period2, "months")
as.numeric(period2, "weeks")
>  26.08929  # OK
as.numeric(period2,"months")
>  0.04166667  # Not OK?

Is this a bug in lubridate? Or something wrong I am doing/missing?

Note: I have seen (old) remark Have lubridate subtraction return only a numeric value, so I guess I may also use workaround to use difftime but I would like to stick to lubridate.

Eric Lecoutre
  • 1,461
  • 16
  • 25
  • 2
    Assuming this is not the desired behavior, I suspect [`seconds_to_unit`](https://github.com/hadley/lubridate/blob/c5ddda2aec8dce0c98523817e8d68b8e9d846297/R/coercion.r#L568) or [`period_to_seconds`](https://github.com/hadley/lubridate/blob/332cd13828e8e20e409967db25e2f175957ddcef/R/periods.r#L517) might be worth exploring. – Roman Luštrik Mar 24 '17 at 08:57

1 Answers1

20

Instead of using as.numeric() try time_length() from lubridate package:

period2= months(6)
time_length(period2,unit="days")
#182.6
time_length(period2,unit="weeks")
#26.09
time_length(period2,unit="months")
#6.004
class(time_length(period2,unit="months"))
#"numeric"
Ishan Juneja
  • 401
  • 4
  • 11