Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 732
asosnovsky Avatar asked Apr 22 '14 15:04

asosnovsky


People also ask

How do I change Dataframe to XTS?

To convert the given dataframe with the date column to the time series object, the user first needs to import and load the xts package. The user then needs to call the xts() function with the required parameters the main need to call this function is to create the time-series object in R language and at the end use is.

What is XTS format?

eXtensible Time Series (xts) is a powerful package that provides an extensible time series class, enabling uniform handling of many R time series classes by extending zoo.


1 Answers

?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))
like image 110
tonytonov Avatar answered Sep 19 '22 14:09

tonytonov