An other possibility is to not change your sqldf
function and then to convert your dates stored as numbers. You can use as.Date()
for this :
zoo::as.Date(16623)
[1] "2015-07-07"
As LyzandeR mentioned, you should specify an origin
which states what the first date is. If you are using the zoo
package, the default is "1970-01-01" and for your format it is probably the correct origin, but if you don't use it (meaning you sue the function from the base
package then you must specify it.
as.Date(16623, origin = "1970-01-01")
[1] "2015-07-07"
But if you had dates from Excel you should change the origin :
zoo::as.Date(42313)
[1] "2085-11-06"
as.Date(42313, origin = "1899-12-30") # for Windows, use "1904-01-01" for Mac
[1] "2015-11-05" # correct result
I actually found why not supplying origin
was working for me : I had the package zoo
loaded, in which "1970-01-01" is the default option for origin
:
base::as.Date(16623)
Error in as.Date.numeric(16623) : 'origin' must be supplied
zoo::as.Date(16623)
[1] "2015-07-07"
Here are the codes where you can see that zoo
specifies a default origin
for the function as.Date.numeric
which is not the case for the base
package :
base::as.Date.numeric
function (x, origin, ...)
{
if (missing(origin))
stop("'origin' must be supplied")
as.Date(origin, ...) + x
}
<bytecode: 0x17190e78>
<environment: namespace:base>
zoo::as.Date.numeric
function (x, origin, ...)
{
if (missing(origin))
origin <- "1970-01-01"
if (identical(origin, "0000-00-00"))
origin <- as.Date("0000-01-01", ...) - 1
as.Date(origin, ...) + x
}
<environment: namespace:zoo>
dput(X)
to provide X in reproducible form. See (1) #5963769 (2) stackoverflow.com/help/mcve (3) stackoverflow.com/help/how-to-ask – Abrahamabrahams