Re: [R] help with POSIX

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Sat 23 Apr 2005 - 05:07:34 EST

On Fri, 22 Apr 2005, Ghosh, Sandeep wrote:

> For the r script below
>
>> datestr <- "01/01/2004"
>> as.POSIXct(as.Date(datestr, "%d/%m/%Y"))
> I get the following output
> "2003-12-31 18:00:00 Central Standard Time"
>
> Why is the date a day before. I guess its something to do with the time,
> but is there a way to get it to return 2004-01-01 instead?

Yes.

If you want POSIXt, which includes time as well as date and so depends on time zone, use it.
> datestr <- "01/01/2004"
> strptime(datestr, "%d/%m/%Y")

[1] "2004-01-01"
> class(strptime(datestr, "%d/%m/%Y"))

[1] "POSIXt" "POSIXlt"
> as.POSIXct(strptime(datestr, "%d/%m/%Y"))
[1] "2004-01-01 Pacific Standard Time"

If you just want a date, just use Date
> as.Date(datestr, "%d/%m/%Y")
[1] "2004-01-01"

When you convert from Date to POSIXt it has to pick an arbitrary time within the day, and it picks midnight GMT.

         -thomas



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Apr 23 05:12:26 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:22 EST