Skip to content

POSIX Date-Time Classes and missing values

3 messages · Bill Oliver, Peter Dalgaard, Brian Ripley

#
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.
[1] "NA"                  "1992-02-27 22:29:56" "1992-01-14 01:03:30"
Error in fromchar(x) : character string is not in a standard unambiguous
format
Also, I haven't found a way to assign missing values to POSIX date-time
entries. For example, the following doesn't work.
[1] "1992-02-27 22:29:56 Mountain Standard Time" "1992-01-14 01:03:30
Mountain Standard Time"
Error in as.POSIXct.default(value) : Don't know how to convert `value' to
class "POSIXct"

I'm prepared to convert the missing values to an arbitrary date and then
test for that date when the time intervals are to be calculated. This
method, however, seems very inelegant and prone to error--I'm hoping that
there is a better method.

The version of R that I'm using is 1.3.0 under Windows 2000.

Thanks!  Bill





-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
"Bill Oliver" <wloliver at qwest.net> writes:
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.
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"
#
On 24 Jun 2001, Peter Dalgaard BSA wrote:

            
but strptime("NA", someformat) does, as one would expect.
It's deliberate.  We need to distinguish NA from an invalid date,
so you have to suply a valid rhs.