R - Nearest non-zero neighbours from a matrix

28 Views Asked by At

I have a table of how closely areas are related on given variables. I would now like to extract the names of the neighbouring area.

matrix <- data.frame(Area =c("A","B","C","D"),A=c(0.0,0.1,0.1,0.9),B=c(0.1,0.0,1,0.7),C=c(0.1,1,0.0,0.3),D=c(0.9,0.7,0.3,0.0)) %>%
    remove_rownames() %>%
    column_to_rownames(var="Area")

I am not quite sure how to do this anyway, but what could complicate matters is that the it needs to be the non-zero minimum to account for the diagonal and there is potential that two areas could have the same value. The sort of result I would be looking for from the above data is something like:

Area ClosestN
A B;C
B A
C A
D C

Thanks all in advance.

0

There are 0 best solutions below