R语言循环填充矩阵报错求助:OPEN、CLOSE等字段计算异常
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) is900 * 0.019(NET_CONTRIBUTION * RETURN), not900*(1+0.019)as you wrote. - Your sample
SAVING_RETURN(0 for row 1) is0 * 0.019(OPEN * RETURN), not0*(1+0.019). - The second row's
OPEN(917.1) matches0 + 900 +17.1 +0— which isOPEN + 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

