简体   繁体   中英

Why does anytime return NA in some cases

Why does anytime return NA in the first case but a valid result in the second case.

 str(A$X1)
 int [1:23744] 1487319525 1487323025 1487325865 1487332405 1487334780 1487353675 1487354135 1487354185 1487354195 1487354215 ...
> anytime(A$X1[1])
[1] NA
> anytime(1487319525)
[1] "2017-02-17 02:18:45 CST"

That is a difference between integer and numeric . Note that your vector is listed as integer .

So witness:

R> library(anytime) R> R> ivec <- c(1487319525L, 1487323025L, 1487325865L) R> str(ivec) int [1:3] 1487319525 1487323025 1487325865 R> anytime(ivec) [1] NA NA NA R> R> fvec <- as.numeric(ivec) ## convert to numeric R> str(fvec) num [1:3] 1.49e+09 1.49e+09 1.49e+09 R> anytime(fvec) [1] "2017-02-17 02:18:45 CST" "2017-02-17 03:17:05 CST" "2017-02-17 04:04:25 CST" R>

We walk a fine line here. Numeric values are taken a POSIXct if in a certain range. Because C++ is strongly typed, your integer vector does make it to that test.

Moreover, we cannot just convert integer to numeric as we rely on this to work too:

R> anytime(20170217)
[1] "2017-02-17 CST"
R> 

Edit: I caved. The current development version in github does:

R> library(anytime)
R> ivec <- c(1487319525L, 1487323025L, 1487325865L)
R> anytime(ivec)
[1] "2017-02-17 02:18:45 CST" "2017-02-17 03:17:05 CST" "2017-02-17 04:04:25 CST"
R>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM