[R] as.POSIXct problem

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Mar 5 19:14:48 CET 2004


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,  http://www.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




More information about the R-help mailing list