-1

I have created a neighboring list from mydata using:

neighbors <- get.knn(mydata, k=5)

it looks like this:

      [,1] [,2] [,3] [,4] [,5]
[1,]   12   80   39   82   41
[2,]  133   52   10   58  150
[3,]   47   59   18  129   72
[4,]   48  150   84  162  155
[5,]    6   50   65   90  105
[6,]   50   90   65   54  105
[7,]  125   10  133  130  134
[8,]    9   93   49   95   53
[9,]    8   94   93   50   49
[10,]  125    7  131   98   58
[11,] etc ...

how can I create an adjacency matrix from this list?

Coav
  • 1
  • 1
  • There is no list that I see. It looks like a matrix. These are distinct objects and it is important to differentiate them as the solutions regarding each tend to be different. – lmo Oct 28 '17 at 12:03

2 Answers2

0

Does the following help:

Let knn matrix be as follows:

lines <- "
   12   80   39   82   41
  133   52   10   58  150
   47   59   18  129   72
   48  150   84  162  155
    6   50   65   90  105
   50   90   65   54  105
  125   10  133  130  134
    9   93   49   95   53
    8   94   93   50   49
  125    7  131   98   58
"
t <- read.table(text=lines,header=FALSE)

And let there be a sparse matrix:

library('Matrix')
m <- Matrix(0,nrow=200,ncol=200,sparse=TRUE)

Now update each element where is nearest neighbor to obtain such matrix showing nearest neighbors:

for (i in 1:dim(t)[1]) {
  for (j in 1:dim(t)[2]) {
    m[i,t[i,j]] = 1
  }
}
> m
200 x 200 sparse Matrix of class "dgCMatrix"

 [1,] . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . 1 . . . . . . . . . . . . . . . . ......
 [2,] . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . ......
 [3,] . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . ......
 [4,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . ......
 [5,] . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . ......
 [6,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . 1 . . . ......
 [7,] . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
 [8,] . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . . . 1 . . . . ......
 [9,] . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 . . . . . . . ......

 ..............................
 ........suppressing columns and rows in show(); maybe adjust 'options(max.print= *, width = *)'
 ..............................


[193,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
[194,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
[195,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
[196,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
[197,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
[198,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
[199,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
[200,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
Heikki
  • 2,214
  • 19
  • 34
0

You can use a 2-mode sociomatrix to create the adjacency matrix. Here is a detailed example.

lines <- "
12   80   39   82   41
133   52   10   58  150
47   59   18  129   72
48  150   84  162  155
6   50   65   90  105
50   90   65   54  105
125   10  133  130  134
9   93   49   95   53
8   94   93   50   49
125    7  131   98   58
"
df <- read.table(text = lines, header = FALSE)
# create groups of neighboring
df <- cbind(df, group = rownames(df))
df <- reshape2::melt(df, id.vars = "group")
df <- df[, c(3, 1)]
df # each value is now associated to at least one group

# create a 2-mode sociomatrix
mat.2mode <-  table(df)
mat.2mode # group(s) of each value

# create adjacency matrix as product of the 2-mode sociomatrix
adj.mat <- mat.2mode %*% t(mat.2mode)
adj.mat # final adjacency matrix
# if you want the diagonal to be 0
# diag(adj.mat) <- 0 

Hope that helps.

nghauran
  • 6,648
  • 2
  • 20
  • 29