Merging XTS objects [closed]

穿精又带淫゛_ 提交于 2019-12-23 05:27:23

问题


I have 5 XTS objects. Each containing an individual variable returns (various asset classes, e.g. stocks, bonds, hedge funds, etc...) The problem I am having is that the date sequence for each object are not exact. Some dates are missing for some of the individual objects. Generally, the data are daily from July 2004 to December 2014, but there are some dates in the XTS object of stock returns that do not exist in the XTS object of bond returns, and vice versa.

How can I merge the 5 XTS objects so that the resultant object contains all the dates in order, as they are currently, and simply generates "NA" for any return series that has a missing observation for that particular date?

I have been trying merge, reclassifying into a data.frame and doing cbind, but nothing seems to work. What can I do to merge all 5 objects and generate NA for missing returns for the series that do not have observations where other series do?

Thanks for whatever help you can provide.


回答1:


I think you are looking for something like this?:

 library(xts)
a <- xts(order.by = Sys.Date()-1:10,(1:10))
b <- xts(order.by = Sys.Date()-5:15,(10:20))
merge(a,b,all=TRUE)

gives:

            a  b
2015-01-06 NA 20
2015-01-07 NA 19
2015-01-08 NA 18
2015-01-09 NA 17
2015-01-10 NA 16
2015-01-11 10 15
2015-01-12  9 14
2015-01-13  8 13
2015-01-14  7 12
2015-01-15  6 11
2015-01-16  5 10
2015-01-17  4 NA
2015-01-18  3 NA
2015-01-19  2 NA
2015-01-20  1 NA

Hope that gets you in the right direction...



来源:https://stackoverflow.com/questions/28075555/merging-xts-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!