R邻接矩阵

R邻接矩阵,r,knn,adjacency-matrix,R,Knn,Adjacency Matrix,我使用以下方法从mydata创建了一个相邻列表: 邻居是否有以下帮助: 设knn矩阵如下: 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

我使用以下方法从mydata创建了一个相邻列表:


邻居是否有以下帮助:

设knn矩阵如下:

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)

可以使用2模式矩阵创建邻接矩阵。下面是一个详细的例子

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 

行我没有看到任何列表。它看起来像一个矩阵。这些是不同的对象,区分它们很重要,因为每个对象的解决方案往往不同。
library('Matrix')
m <- Matrix(0,nrow=200,ncol=200,sparse=TRUE)
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,] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ......
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