Converting data.frame to xts order.by requires an

2019-02-13 14:54发布

问题:

I have this following data frame:

> 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

I am trying to make it into a xts file using

> table3<-xts(table[,-1],order.by=table$Date)

but I get this error:

Error in xts(table[, -1], order.by = table$Date) : 
  order.by requires an appropriate time-based object

Where did I go wrong? I thought that the table$Date were organized as time-based.

回答1:

?xts says that the following about order.by:

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

So an extra explicit conversion is required, e.g. to 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

Another option:

xts(table[, -1], order.by=as.Date(table$Date))


回答2:

An alternative using the tidyquant package is to use as_xts() which is specifically designed to convert data frames to xts objects. Just specify date_col as the column that contains the date. If the class of the date column is character, it will need converted from character to date class with mutate(date = ymd(date)) or something of the sort. Here's how to use 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