11

Is there a way to set default origin for as.Date? I did this function to workaround:

as.date=function(x, origin='1970-01-01') as.Date(x, origin=origin)

For example:

as.Date(0)
Error in as.Date.numeric(0) : 'origin' deve ser especificado

as.date(0)
[1] "1970-01-01"
xm1
  • 1,663
  • 1
  • 17
  • 28

4 Answers4

15

The zoo package adds a default origin:

library(zoo)

as.Date(0)
## [1] "1970-01-01"

Update

This is several years later but it looks like R has added .Date so we can now do this using only base R.

.Date(0)
## [1] "1970-01-01"
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • 1
    I don´t know if I´m mingy, but I avoid to load packages for little things. – xm1 Jan 13 '18 at 17:13
  • @xm1, that's perfectly reasonable, but it also means you miss out on some little things, like default origin for Dates. You could, of course, look *how* the `zoo` package does it and copy it. – Gregor Thomas Jan 13 '18 at 17:27
  • 1
    @GregorThomas, I´ve done my `as.date=function(x) as.Date(x,origin='1970-01-01')` – xm1 Mar 13 '20 at 20:40
4

There is an elegant and simple solution, like zoo, but allows for some tweaking if you need it:

require(anytime)

The base is simply:

anytime(0) which returns for me in eastern standard time:[1] "1969-12-31 19:00:00 EST"

If you want to be able to force it to the UTC center of the temporal universe

anytime(0, asUTC=TRUE)

which returns

[1] "1970-01-01 UTC"

And if you want to tell R that you your data is from a given time zone :

Sys.setenv(TZ= 'desiredTimeZone') with anytime:::getTZ() as your desired time zone if that is the one, in which, your dates were gathered.

Any of the answers will work, this one just gives you control over the integer (or string) of numerals as well as the time zone...so it is pretty universally useful if you are working with data gathered remotely.

sconfluentus
  • 4,693
  • 1
  • 21
  • 40
3

Not really. There is no way the origin date can be changed and remain applicable forever in a session.

If you look at parameters for as.Date (i.e. function then origin does not has a default value when x is in numeric.

## S3 method for class 'numeric'
as.Date(x, origin, ...)

Perhaps, it would have been a good extension to as.Date function to provide default value for origin.

OP has done write thing to create a wrapper function to remove dependency on origin. Perhaps the function can be improved slightly like:

Modified function based on suggestions from suggestions from @sm1 and @Gregor.

## if date.origin is not defined then origin will be taken as "1970-01-01
options(date.origin = "1970-01-01")
as.date <- function(x, origin = getOption("date.origin")){
  origin <- ifelse(is.null(origin), "1970-01-01", origin)
  as.Date(x, origin)
}

## Results: (When date.origin is not set)
## > as.date(0)
## [1] "1970-01-01"
## > as.date(2)
## [1] "1970-01-03"
## Results: (When date.origin is set)
## > options(date.origin = "1970-01-05")
## > as.date(2)
## [1] "1970-01-07"
MKR
  • 19,739
  • 4
  • 23
  • 33
  • 1
    won´t it be easier just `as.date <- function(x, origin = ORIGIN)`? – xm1 Jan 13 '18 at 16:47
  • @xm1 That's good point. But we have consider a case when `ORIGIN` is not defined. Function should not crash or return errors in that case. – MKR Jan 13 '18 at 17:01
  • 1
    Probably would be better to set/get an option than use something in the global environment. – Gregor Thomas Jan 13 '18 at 17:28
  • @Gregor good suggestion. I was about to work on handling cases when `ORIGIN` is not defined but seems `option` is a better choice. – MKR Jan 13 '18 at 17:33
3

The lubridate package has been specically made the work with dates easier :

library(lubridate)
as_date(0)
#[1] "1970-01-01"
denrou
  • 630
  • 3
  • 12