thanks for any help regarding:
why dividing by a constant an xts object in R results in a matrix object (the Date/index column is lost)? I would expect that the resulting object would be an xts object, like the input xts, with just the column values divided by the constant. Why this happens and how to avoid it?
Example of the problem that I observe.
Code extract:
TradeProfit <- as.xts(ifelse(is.na(lag(GLD$Signal, 1)) == 0 & GLD$Signal != lag(GLD$Signal, 1), GLD$TotTradCapital, NA ))
TradeProfit <- na.omit(TradeProfit)
TradeProfitPerc <- ifelse(is.na(lag(TradeProfit,1)) == 0, TradeProfit / 100, NA)
TradeProfitPerc <- na.omit(TradeProfitPerc)
> class(TradeProfit)
[1] "xts" "zoo"
> head(TradeProfit)
Signal
2012-08-15 0.000000
2012-11-02 6.970001
2012-11-06 6.970001
2013-08-09 46.410003
2013-08-14 46.410003
2013-09-27 46.380004
head(TradeProfitPerc)
Signal
[1,] 0.06970001
[2,] 0.06970001
[3,] 0.46410003
[4,] 0.46410003
[5,] 0.46380004
[6,] 0.46380004
> class(TradeProfitPerc)
[1] "matrix"
>
The problem is that TradeProfitPerc looses the Date column/index which I want to retain. Also as TradeProfit is an xts object why the division causes such a strange behavior transforming an xts into a matrix object? How can I keep the xts properties? Thanks!