The Art of R Programming

(WallPaper) #1
There are several possibilities here. But since ultimatelyimin()is called,
we can check them all from within that function. So, let’s set the debug sta-
tus ofimin(), quit the debugger, and rerun the code.

Browse[1]> Q
> debug(imin)
> mind(m)
debugging in: FUN(newX[, i], ...)
debug at cities.R#17: {
n <- length(x)
i <- x[n]
j <- which.min(x[(i + 1):(n - 1)])
return(c(j, x[j]))
}
...

So, we’re inimin(). Let’s see if it properly received the first row ofdd,
which should be (0,12,5,1).

Browse[4]> x
[1] 0 12 5 1

It’s confirmed. This seems to indicate that the first two arguments to
apply()were correct and that the problem is instead withinimin(), though
that remains to be seen.
Let’s single-step through, occasionally typing confirmational queries:

Browse[2]> n
debug at cities.r#17: n <- length(x)
Browse[2]> n
debug at cities.r#18: i <- x[n]
Browse[2]> n
debug at cities.r#19: j <- which.min(x[(i + 1):(n - 1)])
Browse[2]> n
debug at cities.r#20: return(c(j, x[j]))
Browse[2]> print(n)
[1] 4
Browse[2]> i
[1] 1
Browse[2]> j
[1] 2

Recall that we designed our callwhich.min(x[(i + 1):(n - 1)]to look only
at the above-diagonal portion of this row. This is because the matrix is sym-
metric and because we don’t want to consider the distance between a city
and itself.

298 Chapter 13

Free download pdf