[R] Different output type with as.Date

Dirk Eddelbuettel edd @end|ng |rom deb|@n@org
Fri May 16 17:33:15 CEST 2025


On 15 May 2025 at 06:56, Barthelemy Tanguy via R-help wrote:
| Hello,
| 
| I'm encountering what seems to me to be an anomaly with the as.Date() function. If I fill in an origin or not, the function will not return the same type of object (integer or double).
| 
| Here's a small example:
| 
| ``` r
| as.Date(0L) |> typeof()
| #> [1] "integer"
| as.Date(0L, origin = "1970-01-01") |> typeof()
| #> [1] "double"
| ```
| 
| Is it a bug or the intended behaviour? I don't see any mention of this in the documentation.
| I'm working on R 4.5

It is the intended behavior. A 'Date' object can present 'whole' (ie integer)
or 'fractional' days since the epoch. There is no requirement about what
internal representation is used.

>From help(Date) under Details:

     It is intended that the date should be an integer value, but this
     is not enforced in the internal representation.  Fractional days
     will be ignored when printing.  It is possible to produce
     fractional days via the ‘mean’ method or by adding or subtracting
     (see Ops.Date).

Best, Dirk

-- 
dirk.eddelbuettel.com | @eddelbuettel | edd using debian.org



More information about the R-help mailing list