0

I am trying to run an R loop on an individual based model. This includes two lists referring to grid cells, which I originally ran into difficulties with because they returned the error: Error: (list) object cannot be coerced to type 'double'. I think I have resolved this error by typing "as.numeric(unlist(x))."

Example from code:

List 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.'

List 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.' 

However, now when I use these two number arrays (if that's what they are) my code returns the error

Error in dcells[[dredging[whichD]]] : subscript out of bounds

I believe this error is referring to the following lines of code:

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

}

where: whichD<-1
julday<-1+day 'dstart=c(1,25,75,100)dend=c(20,60,80,117)`

Here is the full block of code:

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
}

I was wondering if anyone could show me what is going wrong? I apologize if this is a very simple mistake I am making, I am a novice learner that has been scouring the internet, manuals, and Stack for days with no luck!

Thanks in advance!

Rosie
  • 1
  • 2
  • `whichD` is not defined in your example. Please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – jogo Feb 21 '18 at 11:39
  • Your code is not reproducible. Add more details – Terru_theTerror Feb 21 '18 at 11:42
  • You code is not reproducible and very unclear of what it does, as there error says you are trying to get element from a list which is not there. Here is an example `l <- list(); l[[1]]` => `Error in l[[1]] : subscript out of bounds` – Aleh Feb 21 '18 at 12:01

0 Answers0