Keeping Character Types/Names in xts object in R

萝らか妹 提交于 2019-12-25 08:56:41

问题


Okay so this is probably a really dumb question, but I've check around and couldn't find anything on it so I thought I'd ask here. I'm looking at stock prices for a range of different stocks and I would like to use the xts package, but the problem is that xts removes names/character data types from datasets, such that a dataframe such as this:

TIME     | STOCK  |   PRICE
-----------------------------
.....    | QQQQ   |   .....
.....    | WWWW   |   .....
.....    | EEEE   |   .....

gets converted into an xts object, the STOCK column gets removed so that the xts object now looks like this:

 TIME    |   PRICE
--------------------
 .....   |   .....
 .....   |   .....
 .....   |   .....

What's a good way for R to keep track of the names of the stock for each time and price if I am using xts?

Ideally, I would like to use the as.xts() method - in particular here's the code I used to convert a data table to an xts object:

OrderBookxts <- as.xts.data.table(OrderBook, name= NULL)

I tried following Tushar's suggestion below by editing the code to the following line:

OrderBookxts <- as.xts.data.table(OrderBook, stocknames=STOCK) 

but I get this warning:

Warning message: In as.xts.data.table(OrderBook, stock = StockNames) : Following columns are not numeric and will be omitted: StockNames, Date

So now I'm not really sure what to do :/ Any help would be appreciated, thanks!

来源:https://stackoverflow.com/questions/45539265/keeping-character-types-names-in-xts-object-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!