r - 将 data.frame 转换为 xts order.by 需要一个适当的基于时间的对象

标签 r error-handling dataframe time-series xts

我有以下数据框:

> head(table,10)
     Date     Open  High  Low   Close Volume       Adj.Close
1  2014-04-11 32.64 33.48 32.15 32.87 28040700     32.87
2  2014-04-10 34.88 34.98 33.09 33.40 33970700     33.40
3  2014-04-09 34.19 35.00 33.95 34.87 21597500     34.87
4  2014-04-08 33.10 34.43 33.02 33.83 35440300     33.83
5  2014-04-07 34.11 34.37 32.53 33.07 47770200     33.07
6  2014-04-04 36.01 36.05 33.83 34.26 41049900     34.26
7  2014-04-03 36.66 36.79 35.51 35.76 16792000     35.76
8  2014-04-02 36.68 36.86 36.56 36.64 14522800     36.64
9  2014-04-01 36.16 36.86 36.15 36.49 15734000     36.49
10 2014-03-31 36.46 36.58 35.73 35.90 15153200     35.90

我正在尝试使用
> table3<-xts(table[,-1],order.by=table$Date)

但我收到此错误:
Error in xts(table[, -1], order.by = table$Date) : 
  order.by requires an appropriate time-based object

我哪里做错了?我认为 table$Date 是按时间组织的。

最佳答案

?xts说以下关于order.by :

Currently acceptable classes include: ‘Date’, ‘POSIXct’, ‘timeDate’, as well as ‘yearmon’ and ‘yearqtr’ where the index values remain unique.



因此需要额外的显式转换,例如至 POSIXct :
xts(table[, -1], order.by=as.POSIXct(table$Date))
            Open  High   Low Close   Volume Adj.Close
2014-03-31 36.46 36.58 35.73 35.90 15153200     35.90
2014-04-01 36.16 36.86 36.15 36.49 15734000     36.49
2014-04-02 36.68 36.86 36.56 36.64 14522800     36.64
2014-04-03 36.66 36.79 35.51 35.76 16792000     35.76
2014-04-04 36.01 36.05 33.83 34.26 41049900     34.26
2014-04-07 34.11 34.37 32.53 33.07 47770200     33.07
2014-04-08 33.10 34.43 33.02 33.83 35440300     33.83
2014-04-09 34.19 35.00 33.95 34.87 21597500     34.87
2014-04-10 34.88 34.98 33.09 33.40 33970700     33.40
2014-04-11 32.64 33.48 32.15 32.87 28040700     32.87

另外一个选项:
xts(table[, -1], order.by=as.Date(table$Date))

关于r - 将 data.frame 转换为 xts order.by 需要一个适当的基于时间的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23224142/

相关文章:

error-handling - 在RxJava/RxKotlin中,返回Completable.error(Exception())与抛出之间有什么区别?

python - 属性错误 : 'DataFrame' object has no attribute 'group'

java - 基于Java DataFrame去除重复行

r - 如何使用 R 中指定的列名称创建空数据框?

r - 通过R中的模糊多对一字符串匹配匹配两个数据集

python - *有效地*使用 RPy(或其他方式)将数据帧从 Pandas 移动到 R

c - 没有字段宽度限制的scanf()可能会因大量输入数据而崩溃

vba - 如何打开(并保存)Word文档中的所有嵌入式文档?

r - 从 R 中的 GoogleSheets 中清除列表列表

python - 基于条件循环将新行插入 Pandas DF