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