【问题标题】:Storing output from nested for loops in r将嵌套for循环的输出存储在r中
【发布时间】:2013-06-17 23:18:44
【问题描述】:

在频繁搜索和大量论坛阅读之后,我觉得我现在应该已经找到/理解了这个问题的答案,但这仍然让我感到困惑。我在 r 中有两个嵌套的 for 循环,需要将输出保存到一个变量中,但我不完全知道要分配什么以及在哪里分配。代码按我的意愿运行,只是我似乎无法找到如何以一种或另一种形式获得输出。循环的输入是子矩阵列表。输出可以是相同的格式,但会包含循环中发生的变化,或者更理想的格式会包含一个矩阵中的所有行和列。我尝试进行 cbind 以及在循环之外创建变量以稍后存储所有内容(您可能会注意到我在这些方面的注释尝试),但是,就像我说的那样,我仍然有点困惑。任何帮助将不胜感激!

loop.List <- list()
#results.frame <- data.matrix()

ctr <- 0 # creating a counter and zeroing it


for (i in 1:length(subM.List))   { #Looping through each submatrix in the list
  loop.List[[i]] <- list()
  for (j in 2:nrow(subM.List[[i]])){ #Loop through each row of each submatrix in the list
  if ((subM.List[[i]][j, "LAT"] == -180) & #Imputation 1, imputing data points with 0 activity intensity
     (subM.List[[i]][j, "ACTIVITYIN"] ==  0)   & 
     !((subM.List[[i]][j-1, "ACTIVITYIN"]  >  0))[1]) {   #stop imputing at the first occurrence of a value > 0 in activity intensity
        imputeLat <- replace(subM.List[[i]][ ,"LAT"], subM.List[[i]][j,"LAT"], subM.List[[i]][j-1,"LAT"])
        imputeLon <- replace(subM.List[[i]][ ,"LON"], subM.List[[i]][j,"LON"], subM.List[[i]][j-1,"LON"])
        replace.col <- replace(subM.List[[i]][ ,"Impute"], subM.List[[i]][j,"Impute"], 1) #populated impute column. If point is imputed will have a 1
        allComb <- cbind(imputeLat, imputeLon, replace.col)
        ctr <- (ctr + 1)
    }
  }
  #result[[i]] <- allComb
#write.table(results.frame, "C:\\RWorkspace\\newMatrix.txt")
  #return(results.frame)
}

编辑:子矩阵之一的数据样本 该列表包含几个具有不同行数的子矩阵。

FixTYPE          ActivityIn    LAT     LON     Impute
 8                      0   32.81320 -117.2300
 8                      0   32.81324 -117.2301
 8                      1   32.81327 -117.2302
 8                      1   32.81326 -117.2301
 6                      0   32.81324 -117.2300
 6                      0   32.81338 -117.2302
 6                      0   32.81353 -117.2299
 7                      0   -180.000 -180.000
 7                      0   -180.000 -180.000
 7                      0   -180.000 -180.000
 7                      0   -180.000 -180.000
 7                      1   -180.000 -180.000
 7                      2   -180.000 -180.000
 7                      1   -180.000 -180.000
 1                      0   32.81315 -117.2300
 8                      0   32.81318 -117.2300

【问题讨论】:

  • 这可能都可以通过 lapplyapply 完成,但如果无法访问您的数据(或最小示例),则很难具体提供帮助
  • 我建议你编写一个函数foo,它将一个子矩阵作为输入并输出allComb。然后运行lapply(subM.List, foo)do.call(rbind, lapply(subM.List, foo))

标签: r output nested-loops submatrix


【解决方案1】:

类似于在 for 循环范围之外创建 ctr 变量,如果您想保留这些循环的结果,您应该为您尝试捕获的输出创建某种存储变量。如果您可以提供可重现的示例,我们可以为您提供更多帮助。

这是一个简单的例子:

p=10
q=20
M=matrix(seq(1,p*q), p, q)

output=matrix(NA, p,q) # storage matrix
for(i in 1:p){
  for(j in 1:q){
    # do something
    output[i,j] = 2*i + j^2
  }  
}

正如其他人所说,使用apply 函数可能会简化您尝试完成的工作。

【讨论】:

  • 好的,谢谢@David Marx,我想我终于明白了。是的,我需要对应用函数更加熟悉,而不是尽可能多地使用循环。
  • @David Marx,如果我输入一个数据框并希望输出为一个数据框,其中 i 和 j 作为列怎么办?
  • @PolarBear 这听起来像是一个不同的问题。您能否发布一个新主题来询问这个问题并提供演示输入以及您期望输出的样子?在这里链接,我们可以把这个讨论移到那里。
  • @DavidMarx 谢谢这里是:stackoverflow.com/questions/39190052/…
  • @PolarBear:“这个问题是作者自愿删除的。”
猜你喜欢
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-29
  • 1970-01-01
  • 2021-08-18
  • 2023-03-02
  • 1970-01-01
相关资源
最近更新 更多