You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言循环填充矩阵报错求助:OPEN、CLOSE等字段计算异常

Fixing Your R Matrix Calculation Issue

Hey Tom, let's break down the problems with your code and get it working correctly, plus align it with your sample matrix values.

1. Immediate Syntax Error

The error you're seeing is caused by a missing + operator in your CLOSE calculation line. You wrote:

Investment[i,6] <- Investment[i,1] + Investment[i,2] + Investment[i,4] Investment[i,5]

It should be:

Investment[i,6] <- Investment[i,1] + Investment[i,2] + Investment[i,4] + Investment[i,5]

That's why R threw the "unexpected symbol" error — it didn't know how to interpret Investment[i,4] Investment[i,5] without an operator between them.

2. Formula Mismatch with Sample Data

Looking at your sample matrix, there's a discrepancy between your stated formulas and the actual values:

  • Your sample CONTRIBUTION_RETURN (17.1) is 900 * 0.019 (NET_CONTRIBUTION * RETURN), not 900*(1+0.019) as you wrote.
  • Your sample SAVING_RETURN (0 for row 1) is 0 * 0.019 (OPEN * RETURN), not 0*(1+0.019).
  • The second row's OPEN (917.1) matches 0 + 900 +17.1 +0 — which is OPEN + NET_CONTRIBUTION + CONTRIBUTION_RETURN + SAVING_RETURN.

So I'll adjust the code to use these corrected formulas, which align with your sample.

3. Corrected Code with Proper Initialization

First, let's properly initialize your matrix with the known values, then run the loop correctly:

# Step 1: Create and initialize the matrix
row_count <- 6  # Match your sample size; change to 445 if you need 444 iterations
Investment <- matrix(NA, nrow = row_count, ncol = 6)
colnames(Investment) <- c("OPEN", "NET_CONTRIBUTION", "RETURN", "CONTRIBUTION_RETURN", "SAVING_RETURN", "CLOSE")

# Fill in known constant and return values
Investment[, "NET_CONTRIBUTION"] <- 900
Investment[, "RETURN"] <- c(0.019, -0.006, 0.004, 0.004, 0.002, 0.002)
Investment[1, "OPEN"] <- 0

# Step 2: Run the loop to calculate values
for (i in 1:(row_count - 1)) {
  # Calculate current row's CONTRIBUTION_RETURN and SAVING_RETURN
  Investment[i, "CONTRIBUTION_RETURN"] <- Investment[i, "NET_CONTRIBUTION"] * Investment[i, "RETURN"]
  Investment[i, "SAVING_RETURN"] <- Investment[i, "OPEN"] * Investment[i, "RETURN"]
  
  # Calculate current row's CLOSE
  Investment[i, "CLOSE"] <- Investment[i, "OPEN"] + Investment[i, "NET_CONTRIBUTION"] + 
                            Investment[i, "CONTRIBUTION_RETURN"] + Investment[i, "SAVING_RETURN"]
  
  # Set next row's OPEN to current row's CLOSE
  Investment[i + 1, "OPEN"] <- Investment[i, "CLOSE"]
}

# Step 3: Calculate the last row's remaining values (no next row to set OPEN for)
last_row <- row_count
Investment[last_row, "CONTRIBUTION_RETURN"] <- Investment[last_row, "NET_CONTRIBUTION"] * Investment[last_row, "RETURN"]
Investment[last_row, "SAVING_RETURN"] <- Investment[last_row, "OPEN"] * Investment[last_row, "RETURN"]
Investment[last_row, "CLOSE"] <- Investment[last_row, "OPEN"] + Investment[last_row, "NET_CONTRIBUTION"] + 
                                  Investment[last_row, "CONTRIBUTION_RETURN"] + Investment[last_row, "SAVING_RETURN"]

# View the final matrix
print(Investment)

4. Key Notes for Your 444 Iterations

If you're working with a 445-row matrix (since 444 iterations will fill rows 1 to 444, then row 445 needs manual calculation), just change row_count <- 6 to row_count <- 445, and make sure your RETURN vector has 445 values.

This code will correctly propagate the OPEN value from the previous row's CLOSE, and calculate all other columns as per your sample's implicit logic.

内容的提问来源于stack exchange,提问作者TomTr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 14:13:11