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)
}