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

时间:2014-04-22 15:43:04

标签: 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

我正在尝试使用

将其转换为xts文件
> 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

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

2 个答案:

答案 0 :(得分:14)

?xts表示以下关于order.by

的内容
  

目前可接受的课程包括:'日期','POSIXct','timeDate',   以及索引值保留的'yearmon'和'yearqtr'   唯一的。

因此需要额外的显式转换,例如到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))

答案 1 :(得分:-1)

使用tidyquant包的替代方法是使用as_xts(),它专门用于将数据帧转换为xts对象。只需将date_col指定为包含日期的列即可。如果日期列的类是字符,则需要使用mutate(date = ymd(date))或类似的东西从字符转换为日期类。以下是as_xts()

的使用方法


library(tidyquant)
# Get stock prices
stock_prices <- "AAPL" %>%
    tq_get(get  = "stock.prices",
           from = "2007-01-01",
           to   = "2017-01-01")
stock_prices
#> # A tibble: 2,518 × 7
#>          date  open  high   low close    volume adjusted
#>        <date> <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
#> 1  2007-01-03 86.29 86.58 81.90 83.80 309579900 10.85709
#> 2  2007-01-04 84.05 85.95 83.82 85.66 211815100 11.09807
#> 3  2007-01-05 85.77 86.20 84.40 85.05 208685400 11.01904
#> 4  2007-01-08 85.96 86.53 85.28 85.47 199276700 11.07345
#> 5  2007-01-09 86.45 92.98 85.15 92.57 837324600 11.99333
#> 6  2007-01-10 94.75 97.80 93.45 97.00 738220000 12.56728
#> 7  2007-01-11 95.94 96.78 95.10 95.80 360063200 12.41180
#> 8  2007-01-12 94.59 95.06 93.23 94.62 328172600 12.25892
#> 9  2007-01-16 95.68 97.25 95.45 97.10 311019100 12.58023
#> 10 2007-01-17 97.56 97.60 94.82 94.95 411565000 12.30168
#> # ... with 2,508 more rows

# Coerce to xts object
stock_prices %>% 
    as_xts(date_col = date)
#>              open   high    low  close    volume  adjusted
#> 2007-01-03  86.29  86.58  81.90  83.80 309579900  10.85709
#> 2007-01-04  84.05  85.95  83.82  85.66 211815100  11.09807
#> 2007-01-05  85.77  86.20  84.40  85.05 208685400  11.01904
#> 2007-01-08  85.96  86.53  85.28  85.47 199276700  11.07345
#> 2007-01-09  86.45  92.98  85.15  92.57 837324600  11.99333
#> 2007-01-10  94.75  97.80  93.45  97.00 738220000  12.56728
#> 2007-01-11  95.94  96.78  95.10  95.80 360063200  12.41180
#> 2007-01-12  94.59  95.06  93.23  94.62 328172600  12.25892
#> 2007-01-16  95.68  97.25  95.45  97.10 311019100  12.58023
#> 2007-01-17  97.56  97.60  94.82  94.95 411565000  12.30168
#> 2007-01-18  92.10  92.11  89.05  89.07 591151400  11.53987
#> 2007-01-19  88.63  89.65  88.12  88.50 341118400  11.46602
#> 2007-01-22  89.14  89.16  85.65  86.79 363506500  11.24447
#> 2007-01-23  85.73  87.51  85.51  85.70 301856100  11.10325