1

Is there a way to suppress "subscript out of bounds" errors? If I declare a vector in the console like this a <- c(1,2,3) and then access a[5] i get as a response NA. Why do i then get an error when I do the same thing in a script? I was going out of bound on purpose and wanted to detect the NA

Edit: It is a fragment of a simple GA I'm writing.

reproduce <- function(selected, pop_size, p_cross, p_mutation){
  children <- matrix()
  indexes <- rep(1:ncol(selected), length=pop_size)
  for (index in indexes)
  {
    p1 <- selected[,index]
    if(index %% 2 == 1){
      if (index+1 > )
      p2 <- selected[,index+1]
    }
    else {
      p2 <- selected[,index-1]
    }
    if (is.na(p2) == TRUE)
      p2 <- selected[,1]
    child <- crossover(p1, p2, p_cross)
    child <- point_mutation(child)
    cbind(children, child)
  }
  return(children)
}
Lukasz
  • 2,257
  • 3
  • 26
  • 44
  • 1
    How exactly are you running your "script"? Can you please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so we can see exactly what you're doing? – MrFlick Dec 18 '14 at 13:41
  • Are you sure you're not using `a[[5]]` in your script? That one will cause the error you report. – BrodieG Dec 18 '14 at 13:41
  • more likely matrices. – flodel Dec 18 '14 at 13:55

0 Answers0