[R] Why my lapply doesn't work with FUN=as.Date

David Winsemius dwinsemius at comcast.net
Sun Dec 9 03:31:39 CET 2012


On Dec 8, 2012, at 1:34 PM, 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?
>
Problem #1
`sapply` will coerce to matrix or vector and remove the Date class

Problem #2:
You are not supplying a format to as.Date and your dates are not in  
the default formats>

 > sapply(dts, as.Date)
03-04-2012 02-04-2012 30-03-2012 29-03-2012
    -718323    -718688    -708492    -708857

 > sapply(dts, as.Date, format="%d-%m-%Y")
03-04-2012 02-04-2012 30-03-2012 29-03-2012
      15433      15432      15429      15428

 > lapply(dts, as.Date, format="%d-%m-%Y")
[[1]]
[1] "2012-04-03"

[[2]]
[1] "2012-04-02"

[[3]]
[1] "2012-03-30"

[[4]]
[1] "2012-03-29"

> i am very confused!
>
> Thanks a lot for your time in such a freezing weekend!

Problem #3:
You need to move to the California Coast.
-- 
> .

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list