I'm trying to identify the similarity of words contained in column A and produced a matrix using the adist function. I want to choose the words with the shortest distance except on the diagonal line. Therefore, I applied a function that would identifies the second-to-smallest number, but it sometimes include the 0 on the diagonal line, because all numbers on the main diagonal of the matrix is 0 by definition. I want to get the smallest number except for the one on the main diagonal.

```
a1 = df['A']
a2 = df['A']
dist.name <- adist(a1$A, a2$A, partial = TRUE, ignore.case = TRUE)
min.name <- apply(dist.name, 1, function(x)(sort(x)[2]))

```

Below is an example of the dist.name matrix.

```
    a1  a2  a3  a4  a5
a1  0   3   0   3   1
a2  1   0   3   5   0
a3  2   3   0   0   5
a4  3   0   1   0   5
a5  0   3   2   1   0

```

I want to be able to choose [a1, a3] rather than [a1, a1].