0

我正在尝试对不同的降雨事件进行编号!如果一段时间内没有降雨(time.steps.event.end),则每个新事件都会开始(获得一个新数字)。但不知何故,R 给了我一个错误消息。有趣的是,相同的代码可以工作,但测量值列表更短(格式相同)。供您参考:在 1577809 次测量中,R 总是给我 i=1577739 处的错误。

这是我的代码(的错误部分):

 i=1
 rain.index=0
 finedata=rain.series.matrix[,3]

 while(i<(length(finedata)-time.steps.event.end+1)) { 
   if (finedata[i]==0)
     i=i+1 else {

  rain.index=rain.index+1

  rain.series.matrix[(i-max(durations)/20):i,2]=rain.index


  while(any(finedata[(i+1):(i+time.steps.event.end)]>0)) 
  {
    i=i+1
    rain.series.matrix[i,2]=rain.index

  }
  rain.series.matrix[(i+1):(i+time.steps.event.end),2]=rain.index
  i=i+1
}
}

显示以下错误:

Error in while (any(finedata[(i + 1):(i + time.steps.event.end)] > 0,  : 
  missing value where TRUE/FALSE needed

有谁能够帮我?

4

1 回答 1

3

该错误告诉您您正在尝试比较两件事,但其中之一丢失了。

这是一个更简洁,可重复的示例

x <- 1:2
x[3:4]
#[1] NA NA
while(any(x[3:4] > 0)) print(TRUE)
#Error in while (any(x[3:4] > 0)) print(TRUE) : 
#  missing value where TRUE/FALSE needed

也许你可以专门检查这样的 NA

while(!any(is.na(x[3:4])) && any(x[3:4] > 0)) print(TRUE)
于 2013-09-29T13:36:35.403 回答