The Art of R Programming

(WallPaper) #1

But the valuej=2does not confirm. The minimum value in (0,12,5) is
5, which occurs at index 3 of that vector, not index 2. Thus, the problem is
in this line:


j <- which.min(x[(i + 1):(n - 1)])


What could be wrong?
After taking a break, we realize that although the minimum value of
(0,12,5) occurs at index 3 of that vector, that isnotwhat we askedwhich.min()
to find for us. Instead, thati+1term means we asked for the index of the
minimum in (12,5), which is 2.
We did askwhich.min()for the correct information, but we failed to use
it correctly, because we do want the index of the minimum in (0,12,5). We
need to adjust the output ofwhich.min()accordingly, as follows:


j <- which.min(x[(i+1):(n-1)])
k<-i+j
return(c(k,x[k]))


We make the fix and try again.


mind(m)
Error in mind(m) : subscript out of bounds



Enter a frame number, or 0 to exit


1: mind(m)


Selection:


Oh no,anotherbounds error! To see where the blowup occurred this
time, we issue thewherecommand as before, and we find it was at line 13
again. What aboutiandjnow?


Browse[1]> i
[1] 1
Browse[1]> j
[1] 5


The value ofjis still wrong; it cannot be larger than 3, as we have only
three columns in this matrix. On the other hand,iis correct. The overall
minimum value inddis 5, occurring in row 1, column 3.
So, let’s check the source ofjagain, the matrixwmins:


Browse[1]> wmins
[,1] [,2]
[1,] 3 3
[2,] 5 8


Debugging 299
Free download pdf