나는 index()
이 같은 숫자 인 xts
시계열을 가지고 있습니다. 두 개의 계열을 나누어 빈 행렬에 삽입 한 다음 이것을 다른 쌍에 적용하고 시계열을 유지하려고합니다.xts 시간을 유지하기 위해 for 루프를 두 xts 시계열로 나눕니다.
나는 루프에서 다양한 방법을 시도했다 : 열 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
색인 생성이 유지됩니다. 도움에 감사드립니다. 하지 않은 xts
행렬 결과 후속 벡터
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
에 따른 쌍을 분할하려고
> 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"
>
내 코드. rownames
을 정의하려고해도 작동하지 않습니다. 많은 감사합니다!
당신은'close.price.ratio'를 올바르게 미리 할당했지만, xts 객체가 아니라 행렬로 저장하면됩니다. 'close.price.ratio [, i] <-'는 표현식의 RHS가 하나이기 때문에'close.price.ratio'를 xts 객체로 강요하지 않을 것입니다. –