【发布时间】:2023-04-01 02:36:01
【问题描述】:
我有一个包含多列(每列代表一家公司)和多行(由股票价格组成)的矩阵
我想在不使用包裹的情况下计算退货!
我尝试使用双 for 循环,但它不起作用,我收到错误:
“Portf_Returns[i, j] 中的错误
# Trying to compute Returns in a matrix of stock Prices with double for-loop
ClosingPrices <- sample(10,30,10) # I generate some random stock prices
Portf_ClosingPrices <- matrix(ClosingPrices,nrow = 10, ncol = 3) # 3 companies (3 colums) and 10 stock prices for each company
Portf_Returns <- NULL
i <- 1
j <- 1
for (j in 1:3) {
for (i in 1:9) {
Portf_Returns[i,j] <- Portf_ClosingPrices[i+1,j] / Portf_ClosingPrices[i,j] - 1
}
}
Portf_Returns
【问题讨论】: