How to transform an R Matrix into an xts/zoo object? -
ai'm having problem transforming xts derived r matrix xts object after running returns function. here's i've got...
> class(xtsdata) [1] "xts" "zoo" > head(xtsdata) ts_58_20_b_003_003 ts_58_20_s_021_005 ts_58_20_s_034_013 ts_58_20_s_042_021 2011-01-02 10001.0 10000.0 10000 10000 2011-01-03 10387.5 10001.0 10000 10000 2011-01-04 10387.5 10551.0 10000 10000 2011-01-05 10387.5 10562.5 10000 10000 2011-01-06 10387.5 10562.5 10000 10000 2011-01-07 10387.5 10562.5 10000 10000 > tail(xtsdata) ts_58_20_b_003_003 ts_58_20_s_021_005 ts_58_20_s_034_013 ts_58_20_s_042_021 2013-03-05 21199.0 14905 10274.5 8859 2013-03-06 20498.0 14905 10274.5 8859 2013-03-07 20484.5 14905 10274.5 8859 2013-03-08 20957.5 15478 10847.5 9432 2013-03-11 20957.5 15478 10847.5 9432 2013-03-12 20531.5 14776 10847.5 9432
following procedure in rmetrics documentation...
> xtsdatadiff <- xtsdata[, ] / as.numeric( xtsdata[1, ] ) > class( xtsdatadiff ) [1] "xts" "zoo"
when apply returns function lose returned object xts object...
> xtsdata_1 <- returns( xtsdatadiff ) > class(xtsdata_1) [1] "matrix" > tail ( xtsdata_1 ) ts_58_20_b_003_003 ts_58_20_s_021_005 ts_58_20_s_034_013 ts_58_20_s_042_021 2013-03-05 0.0000000000 -0.000099995 0.000099995 0.000000000 2013-03-06 -0.0337266853 0.000099995 0.000000000 0.000000000 2013-03-07 -0.0005588228 0.000000000 0.000000000 -0.000099995 2013-03-08 0.0228280749 0.037722934 0.054169546 0.062774267 2013-03-11 0.0000000000 -0.000099995 0.000099995 0.000000000 2013-03-12 -0.0206362804 -0.046315423 0.000000000 0.000000000
when try re-establish data set xts object error.
so question this... how re-establish data of matrix xts object?
one idea convert matrix again xts
object. using as.xts
or xts
constructor example:
xts(xtsdata_1,as.posixct(rownames(xtsdata_1)))