Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 365 days equal to 80000 years?

Tags:

r

lubridate

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.)

like image 333
Christoph Avatar asked Aug 26 '16 09:08

Christoph


2 Answers

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.

like image 109
csgillespie Avatar answered Nov 12 '22 00:11

csgillespie


According to the maintainer,

[using duration on an interval object] should be disallowed. For conversion use as.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)"
like image 45
Konrad Rudolph Avatar answered Nov 12 '22 00:11

Konrad Rudolph