1

For some reason I do not understand, when I run as.xts to convert from a matrix with a date in rownames, this operation will generate a Date Time in the end. Since this is different from the start indexes merge/cbinds will not work. Can someone point me what am I doing wrong?

> class(x)
[1] "xts" "zoo"
> head(x)
           XLY.Adjusted XLP.Adjusted XLE.Adjusted AGG.Adjusted IVV.Adjusted
2005-07-31  0.042255791  0.017219585   0.17841600  0.010806168   0.04960026
2005-08-31  0.034117087  0.009951766   0.18476766  0.015245222   0.03825968
2005-09-30 -0.029594066  0.008697349   0.22851906  0.009769765   0.02944754
2005-10-31 -0.015653740  0.019966664   0.09314327 -0.012705172   0.01640395
2005-11-30 -0.005593003  0.005932542   0.05437377 -0.005209811   0.03173972
2005-12-31  0.005084193  0.021293537   0.05672958  0.002592639   0.04045477
> head(index(x))
[1] "2005-07-31" "2005-08-31" "2005-09-30" "2005-10-31" "2005-11-30" "2005-12-31"
> temp=t(apply(-x, 1, rank, na.last = "keep"))
> class(temp)
[1] "matrix"
> head(temp)
           XLY.Adjusted XLP.Adjusted XLE.Adjusted AGG.Adjusted IVV.Adjusted
2005-07-31            3            4            1            5            2
2005-08-31            3            5            1            4            2
2005-09-30            5            4            1            3            2
2005-10-31            5            2            1            4            3
2005-11-30            5            3            1            4            2
2005-12-31            4            3            1            5            2
> head(rownames(temp))
[1] "2005-07-31" "2005-08-31" "2005-09-30" "2005-10-31" "2005-11-30" "2005-12-31"
> y=as.xts(temp)
> class(y)
[1] "xts" "zoo"
> head(y)
           XLY.Adjusted XLP.Adjusted XLE.Adjusted AGG.Adjusted IVV.Adjusted
2005-07-31            3            4            1            5            2
2005-08-31            3            5            1            4            2
2005-09-30            5            4            1            3            2
2005-10-31            5            2            1            4            3
2005-11-30            5            3            1            4            2
2005-12-31            4            3            1            5            2
> head(index(y))
[1] "2005-07-31 BST" "2005-08-31 BST" "2005-09-30 BST" "2005-10-31 GMT" "2005-11-30 GMT" "2005-12-31 GMT"
husvar
  • 373
  • 1
  • 3
  • 13

1 Answers1

2

as.xts.matrix has a dateFormat argument that defaults to "POSIXct", so it assumes the rownames of your matrix are datetimes. If you want them to simply be dates, specify dateFormat="Date" in your as.xts call.

y <- as.xts(temp, dateFormat="Date")
IRTFM
  • 258,963
  • 21
  • 364
  • 487
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418