问题
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