I want to create and merge a number of xts objects
The actual data comes from a list (where each time series length can vary) and is converted to xts. For the sake of reproducibility the problem is similar to the following
x=xts()
for(i in 1:3){
n=round(runif(1, 2, 5))
x=merge(x, x=xts(rnorm(n-1), as.Date(2:n)))
}
x
# x x.1 x.2
# 1970-01-03 01:00:00 0.3130791 0.7817730 0.06623756
# 1970-01-04 01:00:00 NA 0.4827522 0.94823558
# 1970-01-05 01:00:00 NA 0.2411705 NA
As you see, I need an initial empty xts object to start the recursion. Despite:
xts(1:3, as.Date(1:3))
# [,1]
# 1970-01-02 1
# 1970-01-03 2
# 1970-01-04 3
when merging generated xts objects with the empty one, the latter is assumed to have a time-based index.
To avoid the time addition, I can create the initial xts item, giving it explicit dummy data.
x= xts(1, as.Date(1))
for(i in 1:3){
n=round(runif(1, 2, 5))
x=merge(x, x=xts(rnorm(n-1), as.Date(2:n)))
}
x=x[,-1]
if(all(is.na(x[1,]))) x=x[-1,]
x
# x.1 x.2 x.3
# 1970-01-03 -0.5098130 -0.57121953 -1.259878
# 1970-01-04 -0.9984952 -0.26467168 -2.413181
# 1970-01-05 3.0207324 -1.06862153 NA
# 1970-01-06 NA 0.05542239 NA
This works but I have to add extra code for cleaning from the dummy data and most likely, beyond the dummy column, you get also an extra row for the fake date.
Do you have a cleverer idea to merge recursively xts objects?