R: Date function in sqldf giving unusual answer (wrong date format?)

前端 未结 2 1076

I am trying to add to a date using sqldf, i know it should be simple but I can\'t figure out what is wrong with my date format. Using:

sqldf(\"select date(mo         


        
2条回答
  •  难免孤独
    2020-12-21 22:43

    SQLite date functions consider dates as days since Nov 24, 4714BC, which means the integer storage of 16770 for the example date of 2015-12-01 in R returns an ancient date somewhere in 4667BC.

    You can figure out that the difference between the R origin of 1970-01-01 and the SQLite origin is 2440588 days. Which means, you can take this constant into account if you want:

    test <- data.frame(model_date=as.Date("12/1/2015",format="%m/%d/%Y"))
    sqldf("select date(model_date + 2440588, '+1 day') as select_date from test")
    #  select_date
    #1  2015-12-02
    

    @HongOoi's answer is probably better, but I thought this might be interesting to know the underlying workings.

提交回复
热议问题