解决代码循环中的“错误:下标越界”

时间:2018-02-21 11:25:04

标签: r loops subscript

我正在尝试在基于个人的模型上运行R循环。这包括两个引用网格单元格的列表,我最初遇到了困难因为它们返回了错误:错误:( list)对象无法强制键入'double'。我想我已经通过输入“as.numeric(unlist(x))解决了这个错误。”

代码示例:

清单1:

dredg<-list(c(943,944,945,946,947,948,949...1744,1745))
dredging<-as.numeric(unlist(dredg)). I refer to 'dredging' in my code, not 'dredg.'

清单2:

nodredg<-list(c(612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631))
dcells<-as.numeric(unlist(nodredg)) I refer to 'dcells' in my code, not 'nodredg.' 

但是,现在当我使用这两个数字数组时(如果这就是它们),我的代码会返回错误

  

dcells中的错误[[dredging [whichD]]]:下标越界

我认为此错误指的是以下代码行:

if(julday>=dstart[whichD] & julday<=dend[whichD] & dredging[whichD]!=0){
 whichcells<-which(gridd$inds[gridd$julday==julday] %in% dcells[[ dredging[whichD] ]]) #identify cells where dredging is occurring

}

其中:  whichD<-1
julday<-1+day 'dstart=c(1,25,75,100) DEND = C(20,60,80,117)`

以下是完整的代码块:

for (i in 1:time.steps){ 
qday <- qday + 1                                                    
for (p in 1:pop){                                                   
if (is.na(dolphin.q[p,1,i]!=dolphin.q[p,2,i])) {                          
  dolphin.q[p,6,i]<-which.max(c(dolphin.q[p,1,i],dolphin.q[p,2,i])) 
}else{                                                           
  dolphin.q[p,6,i]<-rmulti(c(0.5,0.5))                              
}
usP<-usage[p,]                                                    

if(julday>=dstart[whichD] & julday<=dend[whichD] & dredging[whichD]!=0){    
  whichcells<-which(gridd$inds[gridd$julday==julday] %in% dcells[[ dredging[whichD] ]]) 
  usP[whichcells]<-0                                                                  
  usP<-usP/sum(usP)                                                                 #rescale the home range surface to 1
}

我想知道是否有人能告诉我出了什么问题?如果这是一个非常简单的错误我道歉,我是一个新手学习者,已经在互联网,手册和堆栈上搜索了几天没有运气!

提前致谢!

0 个答案:

没有答案