簡體   English   中英

循環遍歷子集對象並將 output 存儲在 R

[英]Looping through subsetted objects and storing output in R

我有 10 個名為output_ens1output_ens10的對象。 我想從每個數據中提取一條數據,對其進行算術運算,然后將其添加到新的 object 中。 我一直在嘗試使用 for 循環和子集。 這是帶有我的循環的示例 object

## Values used elsewhere in model output processing
num_sp <- 46  # Enter the number of species modeled
num_steps <- 36 # Enter the number of months modeled
num_ens <- 10 # Enter the number of runs in the ensemble

## example object of same dimensions
output_ens1 <- matrix(data = c(1:11880), ncol = 330, nrow = 36)

hist <- c(1:num_ens)
for (i in hist)
{
  hist[i] <- as.name(paste0("output_ens", i))[num_steps,8+num_sp*7]/1000
}

這返回

Error in as.name(paste0("output_ens", i))[num_steps, 8 + num_sp * 7] : 
  object of type 'symbol' is not subsettable

我想要 num_steps (第 36 行)和 8+num_sp*7 (第 330 )列值除以 1000 並添加到 object hist 在這個例子中,值 11.88 (11880/1000) 將被設置為hist[1] 。我已經嘗試了幾次迭代,並且相信我的問題是將對象的名稱讀取為子集。 我應該朝哪個方向前進?

改用這個:

get(paste0("output_ens", i))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM