我正在使用一些代码,这些代码使用%*%
运算符将权重向量应用于表示时间序列的向量。我想将 xts 用于时间序列,但%*%
操作员不理解它应该忽略 xts 索引值。
我知道我可以使用coredata()
将我的系列值作为向量提取出来,对这些值进行操作,然后将它们合并回来,但我想知道是否应该使用一个好的原生 xts 函数。
编辑:代码示例说明了我在行为中看到的差异。
library(xts)
data(sample_matrix)
s<-as.xts(sample_matrix)
o_xts<-s$Open
c_xts<-coredata(s$Open)
len <-length(c_xts)
len2<-len/2
xx<-c_xts[1:len]
outp<-0*0:len2
outp[2] <- xx%*%exp((1:(2*len2))*1.i*pi/len2)
#completes without issue
len <-length(o_xts)
len2<-len/2
yy<-o_xts[1:len]
outp<-0*0:len2
outp[2] <- yy%*%exp((1:(2*len2))*1.i*pi/len2)
Warning message:
In outp[2] <- yy %*% exp((1:(2 * len2)) * (0+1i) * pi/len2) :
number of items to replace is not a multiple of replacement length