Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find "origin" of a Date in R

Tags:

date

r

If I have a date that i've converted using the as.Date function, e.g. "2015-01-01" how can I find out what date it is using as reference for its origin? And yes, I tried ?Date, and tried using the default origin, but got days in 1945. I would like to know how to do this generally so I don't have to manually check every time I do operations on dates.

like image 588
riders994 Avatar asked May 08 '15 20:05

riders994


People also ask

How do I find the origin date in R?

From ? Date : "Dates are represented as the number of days since 1970-01-01." So, if it's a Date class object, that's the origin. So it is 1970.

What does origin must be supplied mean in R?

numeric(x) : 'origin' must be supplied. One error you may encounter in R is: Error in as.Date.numeric(x) : 'origin' must be supplied. This error usually occurs when you attempt to convert a number to a date in R, but fail to provide an origin date.

What is the default date format in R?

Note that the default date format is YYYY-MM-DD; therefore, if your string is of different format you must incorporate the format argument. There are multiple formats that dates can be in; for a complete list of formatting code options in R type ? strftime in your console.

How do I view dates in R?

In R programming, if you use Sys. Date() function, it will give you the system date. You don't need to add an argument inside the parentheses to this function.


2 Answers

R package lubridate has a solution. lubridate::origin will return what origin is for you.

like image 119
Jian Avatar answered Sep 21 '22 15:09

Jian


As described in the documentation, the origin argument does not create a corresponding attribute in the Date object; it's just to make input easier from integers (e.g., from Excel, SAS, etc. that use different origins).

A dput confirms that Dates are plain old numbers:

d <- as.Date("2000-01-01")
dput(d)
# structure(10957, class = "Date")

(I'd use str(d) instead, but that sometimes seems to hide information.)

like image 34
Frank Avatar answered Sep 23 '22 15:09

Frank