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 at hotmail.com download! http://toolbar.msn.click-url.com/go/onm00200413ave/direct/01/
Gabor Grothendieck
2004-Jul-08 05:00 UTC
[R] question about seq.dates from chron vs. as.POSIXct
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 at hotmail.com> To: <r-help at 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 at hotmail.com