Sorry for coming so late to this thread. One possible explanation for the
R side is the following...
On Thu, 25 Mar 2010, Marc Schwartz wrote:
On Mar 25, 2010, at 5:41 PM, Joshua Wiley wrote:
Kind of off the thread a bit, but when I do:
as.Date(40182)
I ***do not*** get "2080-01-06". Instead I get an error:
Error in as.Date.numeric(40182) : 'origin' must be supplied
Yes, that is when you use base R's as.Date.numeric() but package "zoo"
overloads that:
R> as.Date(40182)
Error in as.Date.numeric(40182) : 'origin' must be supplied
R> library("zoo")
R> as.Date(40182)
[1] "2080-01-06"
The reason is that "zoo" had an as.Date.numeric() long before base R had
it. It was designed to allow users to switch back and forth between "Date"
and "numeric", i.e.,
R> x <- Sys.Date()
R> identical(x, as.Date(as.numeric(x)))
[1] TRUE
Hence, to be backward compatible with older code, "zoo"'s version of
as.Date.numeric() has a default "origin" while the base R version does not
provide a default.
Best,
Z
Am I the only user who gets picked on in this way, or does it
happen to others as well? The help on as.Date() clearly specifies
that "origin" must be supplied. So how come Anna got the result that
she did?
I also get that error. I believe there was a thread a few years back
discussing the merits of including a default origin, but to my
knowledge, it was never implemented, and there is no way to set a
default (e.g., through options()).
That was actually just recently discussed again, although I think part of it
occurred offlist.
The reason is that the use of as.Date() in this context is intended to be used,
as is the case here, with dates coming from other applications that have been
converted back to a numeric offset from some origin, where it is likely, as is
the case here, that the origin will not be the same as R's.
Thus, there is a reasonable argument to be made to compel the user to know the
origin in use by the application from which the data was obtained. Otherwise,
if the user does not check their data, they will be in trouble.
If one has dates that have come from R and that have been coerced to numeric, for example
via data manipulation (eg. using aggregate(), etc.), one only need to re-set the class
back to "Date":
# from ?as.Date
x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
z <- as.Date(x, "%d%b%Y")
z
[1] "1960-01-01" "1960-01-02" "1960-03-31" "1960-07-30"
# same as unclass(z)
y <- as.numeric(z)
y
[1] -3653 -3652 -3563 -3442
#Note that R's origin is 1970-01-01
as.Date(y, origin = "1970-01-01")
[1] "1960-01-01" "1960-01-02" "1960-03-31" "1960-07-30"
However, all you really need is:
class(y) <- "Date"
y
[1] "1960-01-01" "1960-01-02" "1960-03-31" "1960-07-30"
HTH,
Marc Schwartz
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.