【问题标题】:POSIX representation of dates日期的 POSIX 表示
【发布时间】:2011-07-31 00:32:26
【问题描述】:

我正在尝试使用 R timeSeries 包来读取 csv 文件。

这是我要运行的代码:

prices <- readSeries(file='A2006-2011.csv',head=T,sep=",",format="%m/%d/%Y")

但是,它不能识别 csv 中的日期(下面是它的前几行)

Date    Open    High    Low Close   Volume  Adj.Close
4/4/2011    45.07   45.41   44.36   44.83   2133900 44.83
4/1/2011    44.88   45.48   44.78   45.14   2212300 45.14
3/31/2011   44.92   45.02   44.46   44.78   2194900 44.78
3/30/2011   44.79   45.17   44.65   45.09   2514100 45.09

它会打印以下警告:

Warning message:
In readSeries(file = "A2006-2011.csv", head = T, sep = ",", format = "%m/%d/%Y") :
  Conversion of timestamps to timeDate objects produced only NAs.
  Are you sure you provided the proper format with argument 'format'
  or in the header of your file ?

如果有人可以帮助我,我将不胜感激。

【问题讨论】:

  • 即使您的文件中似乎没有逗号,您使用逗号作为字段分隔符是否正确?

标签: datetime r file-io posix time-series


【解决方案1】:

老实说,我只是将Date 作为字符串读入表格,然后进行快速转换。例如,

> x = c("4/4/2011", "4/4/2011", "3/31/2011", "3/30/2011")
## In your case you would have prices$Date = as.Date(prices$Date, ...
> as.Date(x, "%m/%d/%Y")
[1] "2011-04-04" "2011-04-04" "2011-03-31" "2011-03-30"

【讨论】:

  • 多亏了你,我才真正想通了。即使文件中的日期似乎是“4/4/2011”,当您将其读入 R 时,它们也会变成“2011-04-04”。因此,如果有人遇到从 yahoo 下载的 csv 数据文件同样的问题,输入格式日期的正确方法是 format="%Y-%m-%d"。
【解决方案2】:

试试这个包:speedRhttps://r-forge.r-project.org/projects/speedr。它是一个可视化数据导入器,支持 Date 和 POSIXct 类。您还可以使用它来直观地过滤工作区中现有的 R 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-13
    • 2017-05-26
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 2013-04-09
    相关资源
    最近更新 更多