[R] stfrtime function not returning proper results through sqldf package in R

peter dalgaard pdalgd at gmail.com
Fri Sep 16 17:38:07 CEST 2016


On 16 Sep 2016, at 15:23 , PIKAL Petr <petr.pikal at precheza.cz> wrote:

> Hi Peter
> 
> The devil is in detail
> 
> Data from OP had different format and was transferred to Date object by as.Date, which results in incorrect values (and NA if not transferred)
> df <- data.frame(Date = c("2013/05/25","2013/05/28","2013/05/31","2013/06/01","2013/06/02",
> "2013/06/05","2013/06/07"), Quantity = c(9,1,15,4,5,17,18))
> df$Date<-as.Date(df$Date)
> cbind(df, sqldf("select strftime( '%m', Date) from df"))
> 

To be fair, he actually used as.Date(...., "%Y/%m/%d"), so the values were in fact correct. The issue lay in the conversion to integer.

I can't figure out how those integers are interpreted, though:

> df <- data.frame(date=c(0,1000,2000))
> cbind(df, sqldf("select strftime( '%Y-%m-%d', date) from df"))
  date strftime( '%Y-%m-%d', date)
1    0                  -471-11-24
2 1000                  -471-08-20
3 2000                  -470-05-16

so the Epoch is nothing sensible and negative years are more than a thousand days??


-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list