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

jim holtman jholtman at gmail.com
Fri Aug 24 19:14:44 CEST 2012


I think your first problem is the coersion done by 'c' when you are
mixing objects of various types: you have POSIXct and character.  What
were your expections?

> 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'?

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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



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