1

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.

fibrou
  • 313
  • 1
  • 5
  • 15

1 Answers1

4

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

colemand77
  • 551
  • 4
  • 11
  • That is what I wanted to do, but to merge 5 XTS objects. Thanks to @joshua-ulrich for the "merge.xts" suggestion. I simply overlooked the availability of ".xts" as there is for "merge.zoo..." My bad. Thanks again Joshua! – fibrou Jan 22 '15 at 14:08