Alternative use of sort :
sort(dMat[,1], index.return = TRUE)$ix[1:6]
It would be nice to add set.seed(.) When using random numbers in the matrix so that we can show that the results are identical. I will skip the results here.
Edit (correct solution): The above solution will only work if the first element is always the smallest! Here is the correct solution, which will always give 5 closest values ββto the first element of the column:
> sort(abs(dMat[-1,1] - dMat[1,1]), index.return=TRUE)$ix[1:5] + 1
Example:
> dMat <- matrix(c(70,4,2,1,6,80,90,100,3), ncol=1)
Arun
source share