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? Thanks in advance... -Sandeep [[alternative HTML version deleted]]
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
"Ghosh, Sandeep" <sghosh at lexgen.com> writes:> 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?Just move to Denmark:> datestr <- "01/01/2004" > as.POSIXct(as.Date(datestr, "%d/%m/%Y"))[1] "2004-01-01 01:00:00 CET" The convention is that Date objects are pegged to 0:00 GMT (not sure what the rationale is -- ISOdate uses 12:00 GMT -- but Brian probably thought about it). An easy workaround is to add 12 hours... -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Maybe Matching Threads
- Need some quick help with lattice - barchart
- Help with stripplot
- FW: plot legend outside the grid
- try / tryCatch for download.file( ) within a for loop when URL does not exist
- String concatenation, File Path Handling to pass to download.file( ) [backslash in DOS paths]