[R] POSIXct-coerced NA's not considered NA by is.na()

Alexander Shenkin ashenkin at ufl.edu
Fri Aug 24 20:26:57 CEST 2012


Thanks for your reply, Jim.

On 8/24/2012 12:14 PM, jim holtman wrote:
> I think your first problem is the coersion done by 'c' when you are
> mixing objects of various types: you have POSIXct and character.

Yes, that's something I may have confounded. Still, the warning I'm
getting is "In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion".
 I'm not sure how c()'s coercion works - the warning seems to indicate
that c() is finding as.POSIXct.  That's strange though, since I would
expect to get an error in that case, not just a warning:

> as.POSIXct("b")
Error in as.POSIXlt.character(x, tz, ...) :
  character string is not in a standard unambiguous format

> What were your expections?

I was expecting that the NA resulting from the coercion would result in
a TRUE value when being operated on by is.na().  Instead, I got:

> is.na(date_vec[4])
[1] FALSE

>> x <- Sys.time()
>> str(x)
>  POSIXct[1:1], format: "2012-08-24 13:12:31"
>> y <- c(x, 'b')
>> str(y)
>  POSIXct[1:2], format: "2012-08-24 13:12:31" NA
> Warning message:
> In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
>> dput(y)
> structure(c("1345828351.75", "b"), class = c("POSIXct", "POSIXt"
> ))
>>
> 
> Look at the 'dput' and see that what it is trying to do is to use the
> numeric value changed to a character string as a POSIXct value.  So I
> am not surprised by the error since it is probably not what you
> wanted.  Did you intend to use 'list' instead of 'c'?

I'm a bit confused about how you get that from dput.  Here's what I see:

> dput(date_vec)
structure(c("1345831833", "1345831834", NA, "b"), class = c("POSIXct",
"POSIXt"))

Regardless, I do get the same strange is.na() behavior from the following:

> bad_date = "b"
> class(bad_date) = "POSIXct"
> bad_date
[1] NA
Warning message:
In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
> is.na(bad_date) # shouldn't this be TRUE?
[1] FALSE

As nasty as it may be, shouldn't something showing up as "NA" result in
TRUE when being tested by is.na()?

Just to put some context around this, I was investigating this issue as
I was thinking about converting dataframe columns to dates, and
detecting errors in that conversion.

thanks,
Allie

> 
> On Fri, Aug 24, 2012 at 9:47 AM, Alexander Shenkin <ashenkin at ufl.edu> wrote:
>> Hello folks,
>>
>> I found a strangeness while experimenting with POSIXct vectors and
>> lists.  It seems that coerced NA's aren't "real" NAs, at least as
>> considered by is.na()?
>>
>>> date_vec = c(as.POSIXct(now()), as.POSIXct(now()+1),NA,"b")
>>> date_vec
>> [1] "2012-08-22 15:00:46 COT" "2012-08-22 15:00:47 COT" NA
>>
>> [4] NA
>> Warning message:
>> In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
>>> date_vec[4]
>> [1] NA
>> Warning message:
>> In as.POSIXlt.POSIXct(x, tz) : NAs introduced by coercion
>>> is.na(date_vec[4])
>> [1] FALSE
>>> is.na(date_vec[3])
>> [1] TRUE
>>> is.POSIXct(date_vec[4])
>> [1] TRUE




More information about the R-help mailing list