2

当我尝试阅读vector a包含许多日期的内容时,我收到了警告。

这是vector a我使用write(a,"a.txt"). 由于它相当大,我已将其附在 Google 驱动器上供任何人下载。基本上,它包含从 2012-01-01 到 2012-12-31 的日期,重复了很多次。

https://drive.google.com/file/d/0B12dCpdCVHeSZjA4YmVXNmV6VUU/edit?usp=sharing

我试图这样做并收到一条警告消息。

> head(ymd(a))
[1] "2012-01-01 UTC" "2012-01-01 UTC" "2012-01-01 UTC" "2012-01-01 UTC" "2012-01-01 UTC"
[6] "2012-01-01 UTC"
Warning message:
 7202 failed to parse. 

查看警告消息,很容易假设日期格式错误。但是,YYYY-MM-DD 是 lubridate 支持的格式。当我对向量的一部分做同样的事情时,什么也没有发生。

> head(ymd(a[1:50000]))
[1] "2012-01-01 UTC" "2012-01-01 UTC" "2012-01-01 UTC" "2012-01-01 UTC" "2012-01-01 UTC"
[6] "2012-01-01 UTC"

使用strptimeandas.Date也不会产生任何错误

> head(strptime(a,format="%Y-%m-%d"))
[1] "2012-01-01" "2012-01-01" "2012-01-01" "2012-01-01" "2012-01-01" "2012-01-01"
> head(as.Date(a))
[1] "2012-01-01" "2012-01-01" "2012-01-01" "2012-01-01" "2012-01-01" "2012-01-01"

我的问题是,我需要担心警告消息还是可以安全地忽略它?

4

2 回答 2

2
> b <- which( is.na( ymd(as.character(a[[1]]), tz="UTC") ) )
Warning message:
 7202 failed to parse. 
> head(b)
[1] 122241 122242 122243 122244 122245 122246
> as.character(a[[1]])[head(b)]
[1] "2012-03-01(1)" "2012-03-01(1)" "2012-03-01(1)" "2012-03-01(1)" "2012-03-01(1)"
[6] "2012-03-01(1)"

看起来有一串这样的异常日期被大间隔隔开:

rle( diff(b) )
Run Length Encoding
  lengths: int [1:15] 1183 1 831 1 928 1 605 1 639 1 ...
  values : int [1:15] 1 111657 1 29857 1 26065 1 25111 1 65729 ...
于 2013-11-21T06:02:52.173 回答
0

使用 anydate 包的不同解决方案,这似乎有效。

library(anydate)
text <- anydate(a[, 1])
sum(is.na(text)) 

# 0
于 2017-06-06T12:33:46.827 回答