此R代码生成一个缺少的值,其中存在TRUE/FALSE错误

此R代码生成一个缺少的值,其中存在TRUE/FALSE错误,r,R,此代码生成一个错误: while diff

此代码生成一个错误:

while diff
现在我发现这通常是因为while中的条件计算为NA,因此while无法检查TRUE/FALSE。但在这种情况下,我真的看不出错误是什么。

据我所知,ppm永远不会在while{}中初始化因为‘j’将永远不会= 0。我也看不到“i”在哪里递增。您可以考虑提供头列表$ppm。

我想我可能错了,因为你从未描述过目标,但也许你真正想要的是:

diff.ppm <- diff(list$ppm)
diff.ppm[1:which.min(diff.ppm >= limit)]

第一次通过while循环时:

j是1,ppm[1]被设置为列表$ppm的第i个元素 j增加到2 j现在大于1,所以取ppm[2]与ppm[1]之间的差值,ppm[2]仍然是NA,因为ppm被初始化为NA,并且ppm[2]还没有被触及;结果是NA 现在回到while循环的顶部,比较diff Now NA和固定在0.05的限制…动臂

i <- 1
j <- 1
limit <- 0.05
diff <- 0     ## diff() is a built-in, probably avoid ...
ppm <- rep(NA,20)
list <- data.frame(ppm=1:10)  ## list is a built-in, probably avoid ...
while(i[1]<=nrow(list))   ## why i[1] ??? why not just i ???
{
  while(diff < limit) ##This is where the error is occurring
  {
    if(j==0)  ## as DWin says: never true, j is initialized to 1
       ppm <- rep(NA,length(ppm))
    ppm[j] <- list$ppm[i]      
    j <- j+1
    if(j > 1) {  ## always true if j is initialized to 1 and incremented the first time
      diff <- ppm[j]-ppm[j-1]  ## will be NA if ppm[j-1] is NA
      cat(j,ppm[j-1],ppm[j],diff,"\n")  ## debugging statement!
     }
  }
  print(ppm)
}

调试标记用于有关调试的问题。它不是用于您希望我们在何处为您进行调试。未意识到这一点。请立即删除该标记。如果您希望重新打开此问题,您可以尝试将其重新表述为调试此类问题的好方法?提示:如果手动跟踪逻辑无法完成此操作,请进行调试如下所示的带有cat的语句可能会有所帮助。
i <- 1
j <- 1
limit <- 0.05
diff <- 0     ## diff() is a built-in, probably avoid ...
ppm <- rep(NA,20)
list <- data.frame(ppm=1:10)  ## list is a built-in, probably avoid ...
while(i[1]<=nrow(list))   ## why i[1] ??? why not just i ???
{
  while(diff < limit) ##This is where the error is occurring
  {
    if(j==0)  ## as DWin says: never true, j is initialized to 1
       ppm <- rep(NA,length(ppm))
    ppm[j] <- list$ppm[i]      
    j <- j+1
    if(j > 1) {  ## always true if j is initialized to 1 and incremented the first time
      diff <- ppm[j]-ppm[j-1]  ## will be NA if ppm[j-1] is NA
      cat(j,ppm[j-1],ppm[j],diff,"\n")  ## debugging statement!
     }
  }
  print(ppm)
}