Hi all, I'm having difficulty converting a 'dates' object to a POSIXct object: testDATES<-c(35947,35971,36004,36008,36053,36066) testDATES<-chron(dates=testDATES, format = c(dates = "m/d/y"), origin=c(month = 12, day = 30, year = 1899))>[1] 06/01/98 06/25/98 07/28/98 08/01/98 09/15/98 09/28/98> as.POSIXct(testDATES)[1] NA NA NA NA NA NA>format(as.POSIXct(testDATES), "%m/%d/%Y")[1] NA NA NA NA NA NA I've looked at the as.POSIXct function, traced the problem to ISOdate and then to strptime, e.g., x<-paste(year=1899, month=12, day=30, hour=12, min=0, sec=0)>x >"1899 12 30 12 0 0"strptime(x, "%Y %m %d %H %M %S") [1] NA NA NA NA However, strptime works fine below. dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92") strptime(dates, "%m/%d/%y")>"02/27/92" "02/27/92" "01/14/92" "02/28/92" "02/01/92"I've been reading through a whole suite of emails on the R archives regarding as.POSIXct problems but I have not found anything that solves my particular problem. I need the dates to be of class POSIXct for use in the its package. I'm using R 1.8.1 on Mac OS 10.3.2. Any help is greatly appreciated. Thanks, Brian -- ********************************************************************* Brian Beckage Department of Botany University of Vermont Marsh Life Science Building Burlington, VT 05405
This is a problem of your OS: your example works on all of mine. Can't you change the origin in chron? If not, you should certainly be able to do as.POSIXct(strptime(as.character(testDATES), "%m/%d/%y")) Here's what I think you can do most easily: shift <- julian(1,1,1970, origin=c(month = 12, day = 30, year = 1899)) as.POSIXct(chron(dates=unclass(testDATES) - shift)) On Fri, 5 Mar 2004, Brian Beckage wrote:> Hi all, > > I'm having difficulty converting a 'dates' object to a POSIXct object: > > testDATES<-c(35947,35971,36004,36008,36053,36066) > > testDATES<-chron(dates=testDATES, format = c(dates = "m/d/y"), > origin=c(month = 12, day = 30, year = 1899)) > > >[1] 06/01/98 06/25/98 07/28/98 08/01/98 09/15/98 09/28/98 > > > as.POSIXct(testDATES) > [1] NA NA NA NA NA NA > > >format(as.POSIXct(testDATES), "%m/%d/%Y") > [1] NA NA NA NA NA NA > > I've looked at the as.POSIXct function, traced the problem to ISOdate > and then to strptime, e.g., > > x<-paste(year=1899, month=12, day=30, hour=12, min=0, sec=0) > >x > >"1899 12 30 12 0 0" > > strptime(x, "%Y %m %d %H %M %S") > [1] NA NA NA NA > > However, strptime works fine below. > > dates <- c("02/27/92", "02/27/92", "01/14/92", > "02/28/92", "02/01/92") > strptime(dates, "%m/%d/%y") > >"02/27/92" "02/27/92" "01/14/92" "02/28/92" "02/01/92" > > I've been reading through a whole suite of emails on the R archives > regarding as.POSIXct problems but I have not found anything that > solves my particular problem. I need the dates to be of class > POSIXct for use in the its package. > > I'm using R 1.8.1 on Mac OS 10.3.2. Any help is greatly appreciated. > > Thanks, > Brian > > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, stats.ox.ac.uk/~ripley University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
I was unable to reproduce your error on Windows 2000 R 1.8.1:> testDATES<-c(35947,35971,36004,36008,36053,36066) > > testDATES<-chron(dates=testDATES, format = c(dates = "m/d/y"),+ origin=c(month = 12, day = 30, year = 1899))> > as.POSIXct(testDATES)[1] "1998-05-31 20:00:00 Eastern Daylight Time" [2] "1998-06-24 20:00:00 Eastern Daylight Time" [3] "1998-07-27 20:00:00 Eastern Daylight Time" [4] "1998-07-31 20:00:00 Eastern Daylight Time" [5] "1998-09-14 20:00:00 Eastern Daylight Time" [6] "1998-09-27 20:00:00 Eastern Daylight Time">At any rate, one idea is to create the chron dates relative to the default origin like this:> testDATES<-c(35947,35971,36004,36008,36053,36066) > testDATES.chron <- chron("12/30/1899") + testDATES > as.POSIXct(testDATES.chron)[1] "1998-05-31 20:00:00 Eastern Daylight Time" [2] "1998-06-24 20:00:00 Eastern Daylight Time" [3] "1998-07-27 20:00:00 Eastern Daylight Time" [4] "1998-07-31 20:00:00 Eastern Daylight Time" [5] "1998-09-14 20:00:00 Eastern Daylight Time" [6] "1998-09-27 20:00:00 Eastern Daylight Time" Does that make any difference on your system? --- Date: Fri, 5 Mar 2004 11:35:45 -0500 From: Brian Beckage <Brian.Beckage at uvm.edu> To: <r-help at stat.math.ethz.ch> Subject: [R] as.POSIXct problem Hi all, I'm having difficulty converting a 'dates' object to a POSIXct object: testDATES<-c(35947,35971,36004,36008,36053,36066) testDATES<-chron(dates=testDATES, format = c(dates = "m/d/y"), origin=c(month = 12, day = 30, year = 1899))>[1] 06/01/98 06/25/98 07/28/98 08/01/98 09/15/98 09/28/98> as.POSIXct(testDATES)[1] NA NA NA NA NA NA>format(as.POSIXct(testDATES), "%m/%d/%Y")[1] NA NA NA NA NA NA I've looked at the as.POSIXct function, traced the problem to ISOdate and then to strptime, e.g., x<-paste(year=1899, month=12, day=30, hour=12, min=0, sec=0)>x >"1899 12 30 12 0 0"strptime(x, "%Y %m %d %H %M %S") [1] NA NA NA NA However, strptime works fine below. dates <- c("02/27/92", "02/27/92", "01/14/92", "02/28/92", "02/01/92") strptime(dates, "%m/%d/%y")>"02/27/92" "02/27/92" "01/14/92" "02/28/92" "02/01/92"I've been reading through a whole suite of emails on the R archives regarding as.POSIXct problems but I have not found anything that solves my particular problem. I need the dates to be of class POSIXct for use in the its package. I'm using R 1.8.1 on Mac OS 10.3.2. Any help is greatly appreciated. Thanks, Brian -- ********************************************************************* Brian Beckage Department of Botany University of Vermont Marsh Life Science Building Burlington, VT 05405
Regarding your question about why it worked, your example of strptime not returning correct results on 100+ year old dates on your machine, suggests POSIXt might have problems, in general on your machine, with 100+ year old dates. By using chron's default origin, everything handed to POSIXt is a recent date whereas with respect to the original origin, POSIXt had to decode time periods of 100+ years. Date: Fri, 5 Mar 2004 22:32:56 -0500 From: Brian Beckage <Brian.Beckage at uvm.edu> To: <ggrothendieck at myway.com>, <r-help at stat.math.ethz.ch> Subject: RE: [R] as.POSIXct problem> > >At any rate, one idea is to create the chron dates >relative to the default origin like this: > > > testDATES<-c(35947,35971,36004,36008,36053,36066) >> testDATES.chron <- chron("12/30/1899") + testDATES > > as.POSIXct(testDATES.chron) >[1] "1998-05-31 20:00:00 Eastern Daylight Time" >[2] "1998-06-24 20:00:00 Eastern Daylight Time" >[3] "1998-07-27 20:00:00 Eastern Daylight Time" >[4] "1998-07-31 20:00:00 Eastern Daylight Time" >[5] "1998-09-14 20:00:00 Eastern Daylight Time" >[6] "1998-09-27 20:00:00 Eastern Daylight Time" > >Does that make any difference on your system?This worked on my machine as well! I'm not sure why this made a difference...but thanks. Brian> >--- >Date: Fri, 5 Mar 2004 11:35:45 -0500 >From: Brian Beckage <Brian.Beckage at uvm.edu> >To: <r-help at stat.math.ethz.ch> >Subject: [R] as.POSIXct problem > > >Hi all, > >I'm having difficulty converting a 'dates' object to a POSIXct object: > >testDATES<-c(35947,35971,36004,36008,36053,36066) > >testDATES<-chron(dates=testDATES, format = c(dates = "m/d/y"), >origin=c(month = 12, day = 30, year = 1899)) > >>[1] 06/01/98 06/25/98 07/28/98 08/01/98 09/15/98 09/28/98 > >> as.POSIXct(testDATES) >[1] NA NA NA NA NA NA > >>format(as.POSIXct(testDATES), "%m/%d/%Y") >[1] NA NA NA NA NA NA > >I've looked at the as.POSIXct function, traced the problem to ISOdate >and then to strptime, e.g., > >x<-paste(year=1899, month=12, day=30, hour=12, min=0, sec=0) >>x >>"1899 12 30 12 0 0" > >strptime(x, "%Y %m %d %H %M %S") >[1] NA NA NA NA > >However, strptime works fine below. > >dates <- c("02/27/92", "02/27/92", "01/14/92", >"02/28/92", "02/01/92") >strptime(dates, "%m/%d/%y") >>"02/27/92" "02/27/92" "01/14/92" "02/28/92" "02/01/92" > >I've been reading through a whole suite of emails on the R archives >regarding as.POSIXct problems but I have not found anything that >solves my particular problem. I need the dates to be of class >POSIXct for use in the its package. > >I'm using R 1.8.1 on Mac OS 10.3.2. Any help is greatly appreciated. > >Thanks, >Brian > > >-- >********************************************************************* >Brian Beckage >Department of Botany >University of Vermont >Marsh Life Science Building >Burlington, VT 05405