2015-07-11 66 views
1

出于某种原因,我不明白,当我运行as.xts从rownames中的日期矩阵转换,此操作将生成日期时间在结束。由于这与开始索引不同,merge/cbinds将不起作用。 有人能指点我做错了什么吗?As.XTS从矩阵 - 错误 - 添加时间和时区信息

> 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" 

回答

2

as.xts.matrixdateFormat的论点,即默认为"POSIXct"的,所以假设你的矩阵的rownames是日期时间。如果您希望他们只是日期,请在您的as.xts调用中指定dateFormat="Date"

y <- as.xts(temp, dateFormat="Date") 
+0

感谢您的拼写检查,@BondedDust –

+0

不需要感谢...你救了我很多时间试图查找答案。 –