[R] POSIX Date-Time Classes and missing values

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Sun Jun 24 21:05:16 CEST 2001


"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.

> 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:

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list