I have a lubridate interval
and wanted to get the number of days as integer. However I get the following strange intermediate results:
library("lubridate")
i1 <- interval("2015-01-01 00:00:00", "2016-01-01 00:00:00")
i1 <- interval(ymd_hms("2015-01-01 00:00:00"), ymd_hms("2016-01-01 00:00:00")) # Gives the same result
duration(i1)
# [1] "31536000s (~365 days)"
duration(i1, units = "days")
# [1] "2724710400000s (~86340.86 years)"
Here are two working shortcuts. The latter throws a message "coercing interval to duration" (And I don't know the reason for that...)
> as.numeric(as.duration(i1), units = "days")
[1] 365
> as.numeric(i1, units = 'days')
(R version 3.3.0 dated 2016-05-03, lubridate version 1.5.6.)
You misunderstand the meaning of the units
argument. From the documentation,
units a character string that specifies the type of units that
num
refers to.
where num
is the first argument. So
duration(i1)
is equivalent to
duration(i1, units = "seconds")
However,
duration(i1, units = "days")
means that i1
is measured in days
; hence the very large number.
According to the maintainer,
[using
duration
on aninterval
object] should be disallowed. For conversion useas.duration
.
In addition, the bug in the above code is now fixed by disallowing the function call:
> duration(i1)
Error in .duration_from_num(num, units) :
First argument to `duration` constructor must be character or numeric. Supplied object of class 'Interval'
> as.duration(i1)
[1] "31536000s (~52.14 weeks)"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With