sapply cannot handle date correctly [duplicate]

老子叫甜甜 提交于 2019-12-24 07:37:20

问题


Possible Duplicate:
Why does mapply not return date-objects?

i got the following bizzar problem, I have no problem when I conver one single string to date:

as.Date(alldays[1])

[1] "3-04-20"

however, when I use sapply or lapply, I got a big negative number, does anyone know why it is like this? Thanks!

> (sapply(alldays[1:4], as.Date))


03-04-2012 02-04-2012 30-03-2012 29-03-2012


   -718323    -718688    -708492    -708857

Hi guys, i found the problem, thanks a lot for your help!


回答1:


sapply applies simplify2array (see the documentation). If you look at the code of simplify2array you see r <- as.vector(unlist(x, recursive = FALSE)). as.vector removes all attributes (again see the documentation) including class "Date".

Use as.Date(alldays[1:4],'%d-%m-%Y).



来源:https://stackoverflow.com/questions/13781957/sapply-cannot-handle-date-correctly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!