Converting data.frame to xts order.by requires an appropriate time-based object

后端 未结 2 654
你的背包
你的背包 2020-12-28 08:09

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 2804         


        
2条回答
  •  忘掉有多难
    2020-12-28 08:47

    An alternative using the timetk package tk_xts(). This will convert most objects (data frames with time series column, zoo, etc) to xts.

    Example:


    library(tidyquant)
    library(timetk)
    
    # 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
    #>                     
    #> 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 %>% 
        tk_xts()
    #>              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
    

提交回复
热议问题