1

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!

Community
  • 1
  • 1
James Bond
  • 6,857
  • 16
  • 49
  • 61

1 Answers1

10

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).

Roland
  • 122,144
  • 10
  • 182
  • 276