在R中的形状文件中添加最近的邻居和权重

I've been working on a R script for a while now. The data-frame I have includes n entries (in this case 100), like a 10x10 matrix and represents a map. Each entry has a value of 0 or 1 which indicates to weight to reach the point from a neighboring point. Those entries are supposed to have x nearest neighbors assigned (3 to 8 - nearest neighbors for the df entries) and the respective weight for reaching it.

我有的:

## layer(name)
## 1
## 2
## 3
## ...
## 100

我需要的:

##   from     to    weight
## 1    1      1         0 
## 2    1      2         1
## 3    1     11         1
## 4    1     12         1
## 5    2      1         1  
## 6    2      2         0
## ... ...   ... ...
## 100 100   100         0

我知道有一些函数可以执行knn和weights,但是我可以找到适合我需求的任何解决方案。有人有个好主意吗?

如果我不够清楚,请随时询问更多信息。

真诚的哈迪