问题
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