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

jim holtman jholtman at gmail.com
Sat Aug 25 13:58:10 CEST 2012


If you are going to convert a column from character to POSIXct, I
would assume that you would do it this way and get the results that
you want:

> x <- data.frame(times = c('2012-08-14 12:00', '2011-01-04 08:00', 'b')
+     , stringsAsFactors = FALSE
+     )
> x
             times
1 2012-08-14 12:00
2 2011-01-04 08:00
3                b
> x$newtime <- as.POSIXct(x$times, format = "%Y-%m-%d %H:%M")
> x
             times             newtime
1 2012-08-14 12:00 2012-08-14 12:00:00
2 2011-01-04 08:00 2011-01-04 08:00:00
3                b                <NA>
> is.na(x$newtime)
[1] FALSE FALSE  TRUE
>


On Fri, Aug 24, 2012 at 2:26 PM, Alexander Shenkin <ashenkin at ufl.edu> wrote:
> 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
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.




More information about the R-help mailing list