POSIX表示日期

时间:2011-04-06 17:02:11

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

我正在尝试使用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 ?

如果有人能帮助我,我将非常感激。

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分:0)

试试这个套餐:https://r-forge.r-project.org/projects/speedr的speedR。它是一个可视化数据导入器,支持Date和POSIXct类。您也可以使用它从视觉上过滤工作区中的现有R对象。