我必须 xts/zoo 对象。每个都在不同的时间跨度内测量不同的变量。我想创建一个包含所有度量的单个时间序列,其中 NA 用于缺少日期/变量组合。我怎么做?人工示例:
library(xts)
x<-cbind(a=1:3,b=3:1)
rownames(x) = as.character(Sys.Date()-1:3)
y<-cbind(a=5:7,c=3:1)
rownames(y) = as.character(Sys.Date()-5:7)
xs=as.xts(x)
ys=as.xts(y)
#now what?
#desired outcome looks like:
a b c
2013-03-10 7 NA 1
2013-03-11 6 NA 2
2013-03-12 5 NA 3
2013-03-14 3 1 NA
2013-03-15 2 2 NA
2013-03-16 1 3 NA
# regular merge looks like that (adding an a.1 variable)
merge(xs,ys)
a b a.1 c
2013-03-10 NA NA 7 1
2013-03-11 NA NA 6 2
2013-03-12 NA NA 5 3
2013-03-14 3 1 NA NA
2013-03-15 2 2 NA NA
2013-03-16 1 3 NA NA
# simple concatenation ignores variable names and looks like that
c(xs,ys)
a b
2013-03-10 7 1
2013-03-11 6 2
2013-03-12 5 3
2013-03-14 3 1
2013-03-15 2 2
2013-03-16 1 3
# so what should I do?