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.
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.
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.
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.
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.
R package lubridate
has a solution. lubridate::origin
will return what origin is for you.
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 Date
s 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.)
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