【问题标题】:Fill an object with the results of every loop iteration in R用 R 中每个循环迭代的结果填充对象
【发布时间】:2026-02-22 00:25:02
【问题描述】:

第一次在这里提问。我最近与 R 一起工作,我希望我能在某个问题上得到一些帮助。这个问题可能很容易解决,但我自己无法找到答案,我的研究也没有成功。

基本上我需要根据循环的输入创建一个对象。我有 7 个模拟资产回报,这些对象包含我运行的模拟结果。我想匹配每个对象的列并形成一个组合的列(即每一列 1 形成一个对象),这将用于一些计算。 最后,每次迭代的结果都应该存储在一个对象上,该对象必须在循环外可用以供进一步分析。

我创建了以下循环,问题是只有最后一次迭代的结果被写入最终对象。

# Initial xts object definition
iteration_returns_combined <- iteration_returns_draft_1


for (i in 2:10){

  # Compose object by extracting the i element of every simulation serie
  matrix_daily_return_iteration <- cbind(xts_simulated_return_asset_1[,i],
                                         xts_simulated_return_asset_2[,i],
                                         xts_simulated_return_asset_3[,i],
                                         xts_simulated_return_asset_4[,i],
                                         xts_simulated_return_asset_5[,i],
                                         xts_simulated_return_asset_6[,i],
                                         xts_simulated_return_asset_7[,i])

  # Transform the matrix to an xts object
  daily_return_iteration_xts <- as.xts(matrix_daily_return_iteration,
                                       order.by = index(optimization_returns))

  # Calculate the daily portfolio returns using the iteration return object
  iteration_returns <- Return.portfolio(daily_return_iteration_xts,
                                        extractWeights(portfolio_optimization))

  # Create a combined object for each iteration of portfolio return
  # This is the object that is needed in the end
  iteration_returns_combined <<- cbind(iteration_returns_draft_combined, 
                                       iteration_returns_draft)

}

iteration_returns_combined_after_loop_view

有人可以帮我解决这个问题吗,我将非常感谢任何人提供的任何信息。

谢谢, R-菜鸟

【问题讨论】:

  • 请更正代码“
  • 谢谢!它现在似乎正在工作
  • @RajPadmanabhan,这不是错字。 R中有这样一个assignment operator
  • @Parfait,我现在明白了,谢谢!
  • @Parfait 对,我想我之前在创建函数时使用过,这样函数中的部分结果可以在函数的另一部分中使用

标签: r loops object for-loop xts


【解决方案1】:

通过查看代码,我推测错误出现在 for 循环的最后一行。

iteration_returns_draft_combined

从未定义,因此假定为 NULL。本质上,您只需将每次迭代的结果列绑定到 NULL 对象。因此,您最后一个循环的输出也按列绑定到 NULL 对象,这是您观察到的。请尝试以下操作:

iteration_returns_combined <- cbind(iteration_returns_combined, 
                                   iteration_returns)

这应该可行,希望!

【讨论】:

    【解决方案2】:

    考虑sapply 并避免在循环中扩展对象:

    iteration_returns_combined <- sapply(2:10, function(i) {
    
      # Compose object by extracting the i element of every simulation serie
      matrix_daily_return_iteration <- cbind(xts_simulated_return_asset_1[,i],
                                             xts_simulated_return_asset_2[,i],
                                             xts_simulated_return_asset_3[,i],
                                             xts_simulated_return_asset_4[,i],
                                             xts_simulated_return_asset_5[,i],
                                             xts_simulated_return_asset_6[,i],
                                             xts_simulated_return_asset_7[,i])
    
      # Transform the matrix to an xts object
      daily_return_iteration_xts <- as.xts(matrix_daily_return_iteration,
                                           order.by = index(optimization_returns))
    
      # Calculate the daily portfolio returns using the iteration return object
      iteration_returns <- Return.portfolio(daily_return_iteration_xts,
                                            extractWeights(portfolio_optimization))
    })
    

    如果需要列绑定第一个向量/矩阵,请在之后进行:

    # CBIND INITIAL RUN
    iteration_returns_combined <- cbind(iteration_returns_draft_1, iteration_returns_combined)
    

    【讨论】:

      最近更新 更多