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! [[alternative HTML version deleted]]
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
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