r-将两个xts时间序列划分为保持xts时间的循环



我有多个xts时间序列,其中index()对所有时间序列都是相同的。我想划分两个序列,将它们插入一个空矩阵中,然后将其应用于其他对,并保持时间序列。

我在循环中尝试了各种方法:例如简单地划分列close.price.ratio[,i] <- close.price[,cor.ticker[i,1]]/close.price[,cor.ticker[i,2]]mapply('/',close.price['APC.Close'],close.price['CVX.Close'],SIMPLFY=F)。然而,close.price.ratio矩阵总是失去索引,并且不是xts序列。

只划分两列就可以保持xts索引。感谢您的帮助。

> head(close.price)
           KO.Close PEP.Close EOG.Close NBL.Close CVX.Close XOM.Close COG.Close APC.Close APA.Close
2014-01-02    40.66     82.10    165.02     66.59    124.14     99.75     38.17     78.56     85.48
2014-01-03    40.46     82.24    164.56     66.14    124.35     99.51     37.95     78.31     85.54
2014-01-06    40.27     82.28    164.02     65.75    124.02     99.66     38.20     78.34     86.31
2014-01-07    40.39     83.48    166.85     65.85    125.07    101.07     38.87     79.84     87.90
2014-01-08    39.94     83.24    166.78     65.71    123.29    100.74     38.75     79.04     86.65
2014-01-09    39.73     82.85    167.00     65.35    123.29     99.76     37.44     79.08     86.09
str(close.price)
An 'xts' object on 2014-01-02/2014-10-03 containing:
  Data: num [1:191, 1:9] 40.7 40.5 40.3 40.4 39.9 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:9] "KO.Close" "PEP.Close" "EOG.Close" "NBL.Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
List of 2
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2014-10-06 18:16:04"
> 

我的代码试图根据下面的向量来划分对

head(cor.ticker)
     [,1]        [,2]           
[1,] "PEP.Close" "KO.Close" 
[2,] "CVX.Close" "KO.Close"  
[3,] "APC.Close" "KO.Close"   
[4,] "APA.Close" "KO.Close"   
[5,] "EOG.Close" "PEP.Close" 
[6,] "CVX.Close" "PEP.Close"
close.price.ratio <- matrix(,nrow=nrow(close.price),ncol=nrow(cor.ticker))
for (i in 1:nrow(cor)){
  close.price.ratio[,i] <- close.price[,cor.ticker[i,1]]/close.price[,cor.ticker[i,2]] 
  colnames(close.price.ratio) <- c(cor.ticker[,3])
}
> head(close.price.ratio)
       PEP.KO   CVX.KO   APC.KO   APA.KO  EOG.PEP  CVX.PEP  
[1,] 2.019183 3.053123 1.932120 2.102312 2.009988 1.512058 
[2,] 2.032625 3.073406 1.935492 2.114187 2.000973 1.512038 
[3,] 2.043208 3.079712 1.945369 2.143283 1.993437 1.507292 
[4,] 2.066848 3.096559 1.976727 2.176281 1.998682 1.498203 
[5,] 2.084126 3.086880 1.978968 2.169504 2.003604 1.481139 
[6,] 2.085326 3.103197 1.990435 2.166876 2.015691 1.488111 

从而产生非CCD_ 8矩阵。尝试定义rownames也不起作用。非常感谢!

这似乎有效。

get.column <- function(i) close.price[,corr.ticker[i,1]]/close.price[,corr.ticker[i,2]]
result     <- do.call(cbind,lapply(1:nrow(corr.ticker),get.column))
names(result) <- corr.ticker[,1]
result
#            PEP.Close CVX.Close APC.Close APA.Close EOG.Close CVX.Close
# 2014-01-02  2.019183  3.053123  1.932120  2.102312  2.009988  1.512058
# 2014-01-03  2.032625  3.073406  1.935492  2.114187  2.000973  1.512038
# 2014-01-06  2.043208  3.079712  1.945369  2.143283  1.993437  1.507292
# 2014-01-07  2.066848  3.096559  1.976727  2.176281  1.998682  1.498203
# 2014-01-08  2.084126  3.086880  1.978968  2.169504  2.003604  1.481139
# 2014-01-09  2.085326  3.103197  1.990435  2.166876  2.015691  1.488111

这也是有效的,可能会更快一点。

do.call(cbind,mapply(function(x,y) close.price[,x]/close.price[,y],
                     corr.ticker[,1],corr.ticker[,2],SIMPLIFY=FALSE))

我认为你的问题是,在的声明中

close.price.ratio[,i] <- close.price[,cor.ticker[i,1]]/close.price[,cor.ticker[i,2]]

RHS被评估为xts对象,但由于编写LHS的方式,它被强制为矩阵。使用do.call(cbind,lapply(...))可以通过单独创建列并在最后将它们绑定在一起来避免这种情况。注意,cbind.xts(...)merge.xts(...)相同,它基于index(...)进行匹配。

最新更新