[R] POSIX Date-Time Classes and missing values

Prof Brian D Ripley ripley at stats.ox.ac.uk
Sun Jun 24 23:39:28 CEST 2001


On 24 Jun 2001, Peter Dalgaard BSA wrote:

> "Bill Oliver" <wloliver at qwest.net> writes:
>
> > Is there a good method for dealing with missing values when using the POSIX
> > date-time classes?
> >
> > I would like to convert character data with some missing values to dates, so
> > that I can calculate intervals of time. However, I'm having trouble figuring
> > out a good way to work with the missing values. Missing values cause
> > problems when converting to the POSIXct date-time class, as illustrated
> > below.
> >
> > > foobar
> > [1] "NA"                  "1992-02-27 22:29:56" "1992-01-14 01:03:30"
> > > as.POSIXct(foobar)
> > Error in fromchar(x) : character string is not in a standard unambiguous
> > format
> > >
>
> Hrm. There would seem to be some scope for improvement there...
>
> For now, as.POSIXct(strptime(foobar,format)) will work better. Notice
> though, that it gives NA also for invalid strings and that foobar must
> be a character vector so strptime(NA,format) won't work.

but strptime("NA", someformat) does, as one would expect.

> > Also, I haven't found a way to assign missing values to POSIX date-time
> > entries. For example, the following doesn't work.
> >
> > > foodate
> > [1] "1992-02-27 22:29:56 Mountain Standard Time" "1992-01-14 01:03:30
> > Mountain Standard Time"
> > > foodate[1] <- NA
> > Error in as.POSIXct.default(value) : Don't know how to convert `value' to
> > class "POSIXct"
>
> Again, that probably should be made work eventually, but here's a
> workaround:

It's deliberate.  We need to distinguish NA from an invalid date,
so you have to suply a valid rhs.

>
> x <- c("1jan1960", "2jan1960", "31mar1960", NA)
> z <- strptime(x, "%d%b%Y")
> y <- as.POSIXct(z)
> ctNA <- ISOdate(NA,NA,NA)
> y[1] <- ctNA
> y
>
> # [1] "NA"             "1960-01-02 CET" "1960-03-31 CET" "NA"
>
> --
>    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
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list