RE: [R] question about seq.dates from chron vs. as.POSIXct

From: Gabor Grothendieck <ggrothendieck_at_myway.com>
Date: Thu 08 Jul 2004 - 15:00:55 EST

Yes, its assuming GMT.

I find it best to use an intermediate conversion to character to avoid these sorts of problems. If, as in your example, you just have dates and no times then the following would do it (and has the advantage that you don't have to specify your time zone explicitly so it will still work if someone in another timezone tries your code):

   as.POSIXct(format(as.Date(xt)))

The reason to convert it to Date first, before formatting is so that the format will use Date's default format (which is accepted by as.POSIXct) rather than chron's default format.

---

Date:   	Wed, 07 Jul 2004 15:30:47 -0500
From:   	Laura Holt <lauraholt_983@hotmail.com>
To:   	<r-help@stat.math.ethz.ch>
Subject:   	[R] question about seq.dates from chron vs. as.POSIXct

Dear R People:

Here is an interesting question:


>library(chron)
>xt <- seq.dates(from="01/01/2004",by="days",length=5)
>xt
[1] 01/01/04 01/02/04 01/03/04 01/04/04 01/05/04
>
#Fine so far
>as.POSIXct(xt)
[1] "2003-12-31 18:00:00 Central Standard Time" [2] "2004-01-01 18:00:00 Central Standard Time" [3] "2004-01-02 18:00:00 Central Standard Time" [4] "2004-01-03 18:00:00 Central Standard Time" [5] "2004-01-04 18:00:00 Central Standard Time"
>
Why do the dates change, please? Presumably the as.POSIXct is taking the xt as midnight GMT and converting to Central Standard Time. Is the best solution to:
>as.POSIXlt(xt, "CST")
[1] "2004-01-01 CST" "2004-01-02 CST" "2004-01-03 CST" "2004-01-04 CST" [5] "2004-01-05 CST"
>
Thanks in advance! Sincerely, Laura Holt who is corrupted by dates and times mailto: lauraholt_983@hotmail.com ______________________________________________ R-help@stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Thu Jul 08 15:04:55 2004

This archive was generated by hypermail 2.1.8 : Wed 03 Nov 2004 - 22:54:46 EST