Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R set default origin for as.Date

Tags:

r

as.date

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"
like image 963
xm1 Avatar asked Jan 13 '18 16:01

xm1


People also ask

How do I change the default date format in R?

How to Change Date Formats in R To change the date formats in R, use the format () function. For example, if you want to get the date than the standard %Y-%m-%d, use the format () function from the base package.

Is it possible to set default value for origin in date?

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.

How to create a date in R?

We will continue to use the release date of R 3.6.2, 2019-12-12. Until now, we have stored the above date as character/string but now we will use as.Date () to save it as a Date object. as.Date () is the easiest and simplest way to create dates in R. release_date <- as.Date ("2019-12-12") release_date ## [1] "2019-12-12"

How to get the locale-specific date in R?

Let’s get the locale-specific date in R, using format () and Sys.Date () function. To convert a string to date in R, use the as.Date () function. You can see that we used the as.Date () function to convert character data to dates. The format is as.Date (x, “format“), where x is the character data and format gives the appropriate format.


4 Answers

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"
like image 77
G. Grothendieck Avatar answered Oct 18 '22 01:10

G. Grothendieck


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.

like image 41
sconfluentus Avatar answered Oct 18 '22 00:10

sconfluentus


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"
like image 25
MKR Avatar answered Oct 18 '22 01:10

MKR


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

library(lubridate)
as_date(0)
#[1] "1970-01-01"
like image 29
denrou Avatar answered Oct 18 '22 01:10

denrou