Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Date class in base R backed by a double

The Date class in base R is backed by a double:

> dput(Sys.Date())
structure(18095, class = "Date")
> class(unclass(Sys.Date()))
[1] "numeric"

Why not by an integer? That would save half the memory (4 bytes per date) and seems to work in simple cases at least:

> structure(18095L, class = "Date")
[1] "2019-07-18"

Edit:

Comments mention using fractional dates. I'm wary of using fractional dates in general but R's behaviour might definitely not be what you expect:

> as.POSIXct(structure(18095.5, class = "Date"))
[1] "2019-07-18 14:00:00 CEST"

I can see why that is the correct result but still.

like image 643
Bob Jansen Avatar asked Jul 18 '19 07:07

Bob Jansen


People also ask

How are dates stored in R?

Date objects are stored in R as integer values, allowing for dates to be compared and manipulated as you would a numeric vector. Logical comparisons are a simple. When referring to dates, earlier dates are “less than” later dates.

What class should a date be in R?

Date values can be represented in tables as numbers or characters. But to be properly interpreted by R as dates, date values should be converted to an R date object class or a POSIXct / POSIXt object class.

Is there a date data type in R?

In addition to the time data types R also has a date data type. The difference is that the date data type keeps track of numbers of days rather than seconds. You can cast a string into a date type using the as. Date function.


1 Answers

I stumbled on this post in the data.table issue tracker. There Matt Dowle mentions that R core told him that double is indeed used to support fractional dates and to ensure that the Date class is closed under operations such as mean(). I don't think that is a great idea, luckily for people like me there now is IDateTime :)

like image 92
Bob Jansen Avatar answered Oct 27 '22 00:10

Bob Jansen