[R] Why my lapply doesn't work with FUN=as.Date
Rolf Turner
rolf.turner at xtra.co.nz
Sun Dec 9 03:28:35 CET 2012
On 09/12/12 10:34, CHEN, Cheng wrote:
> Hi, guys
>
> I don't understand why I can apply as.Date to a single item in the list:
>> as.Date(alldays[4])
> [1] "29-03-20"
>
> but when I try to lapply as.Date to all the items, i got a sequence of neg
> numbers:
>
>> sapply(alldays[1:4], FUN=as.Date)
> 03-04-2012 02-04-2012 30-03-2012 29-03-2012
> -718323 -718688 -708492 -708857
>
> does anyone know what's wrong here?
>
> i am very confused!
>
> Thanks a lot for your time in such a freezing weekend!
It's actually a very fine warm sunny weekend here in the
Good Part of the World! :-)
(a) I don't understand the phenomenon you describe either.
(b) However, it seems to me there is no need to us sapply() at
all; just do:
as.Date(alldays)
and you get results as expected. But see below.
(c) You are getting a toadally wrong answer from as.Date(alldays[4]),
if I am reading the output correctly. My understanding is that alldays[4]
is "29-03-2012" and so you *want* to get 2012-03-29 from as.Date
(and *NOT* what you got!).
You need to do as.Date(alldays, format="%d-%m-%Y"). Is it not so?
cheers,
Rolf Turner
More information about the R-help
mailing list