连接两个邻接矩阵并保留值

时间:2019-04-17 20:30:04

标签: r matrix adjacency-matrix

this question之后,是否可以合并两个邻接矩阵并保留新行和新列的值?

以参考文献中的示例为基础,mat1mat2的并集使用0初始化新添加的行和列中的值。我想用邻接矩阵中的值初始化它们,以保留该信息。

> mat1
      Tommy Roy Addy Sam
Tommy     0   1    0  -1
Roy      -1  -1    1   0
Addy      1   0   -1   0
Sam       0   0   -1   1
> mat2
     Mike Roy Addy Sam Dan
Mike    0   1    0  -1   0
Roy    -1  -1    1   0   1
Addy    1   0   -1   0  -1
Sam     0   0   -1   1   0
Dan     1   0    0  -1   1

complete_matrix <- function(mat, ref) {
  dif <- setdiff(rownames(ref), rownames(mat))
  mat <- rbind(mat, matrix(0, length(dif), ncol(mat), dimnames = list(dif, NULL)))
  mat <- cbind(mat, matrix(0, nrow(mat), length(dif), dimnames = list(NULL, dif)))
  return(mat)
}

> complete_matrix(mat2, mat1)
      Mike Roy Addy Sam Dan Tommy
Mike     0   1    0  -1   0     0
Roy     -1  -1    1   0   1     0
Addy     1   0   -1   0  -1     0
Sam      0   0   -1   1   0     0
Dan      1   0    0  -1   1     0
Tommy    0   0    0   0   0     0

例如,我希望complete_matrix(mat2, mat1)产生以下内容(观察Tommy行和列):

      Mike Roy Addy Sam Dan Tommy
Mike     0   1    0  -1   0     0
Roy     -1  -1    1   0   1     1
Addy     1   0   -1   0  -1     0
Sam      0   0   -1   1   0    -1
Dan      1   0    0  -1   1     0
Tommy    0   1    0  -1   0     0

dput for c&P:

mat1 <- structure(c(0L, -1L, 1L, 0L, 1L, -1L, 0L, 0L, 0L, 1L, -1L, -1L, 
            -1L, 0L, 0L, 1L), .Dim = c(4L, 4L), .Dimnames = list(c("Tommy", 
                                                                   "Roy", "Addy", "Sam"), c("Tommy", "Roy", "Addy", "Sam")))
mat2 <- structure(c(0L, -1L, 1L, 0L, 1L, 1L, -1L, 0L, 0L, 0L, 0L, 1L, 
                    -1L, -1L, 0L, -1L, 0L, 0L, 1L, -1L, 0L, 1L, -1L, 0L, 1L), .Dim = c(5L, 
                                                                                       5L), .Dimnames = list(c("Mike", "Roy", "Addy", "Sam", "Dan"), 
                                                                                                             c("Mike", "Roy", "Addy", "Sam", "Dan")))

2 个答案:

答案 0 :(得分:3)

您可以使用union软件包的igraph函数来完成此操作,这需要先将矩阵转换为图形,然后再将结果图形转换回矩阵:

library(igraph)

g1 = graph_from_adjacency_matrix(mat1,weighted=T)
g2 = graph_from_adjacency_matrix(mat2,weighted=T)
g3 = union(g1,g2)

union不会自动合并g1和g2的权重,而是将它们保留为单独的属性weight_1weight_2。我们可以通过取两个权重之间的最小值来合并它们,如果两个矩阵之间没有差异,则这只是合并并删除NA值的一种方法。

E(g3)$weight = pmin(E(g3)$weight_1,E(g3)$weight_2,na.rm=T)
res = as.matrix(as_adj(g3,attr="weight"))

      Tommy Roy Addy Sam Mike Dan
Tommy     0   1    0  -1    0   0
Roy      -1  -1    1   0   -1   1
Addy      1   0   -1   0    1  -1
Sam       0   0   -1   1    0   0
Mike      0   1    0  -1    0   0
Dan       0   0    0  -1    1   1

答案 1 :(得分:2)

我会

  1. 将矩阵转换为单个对矩阵(即对值),即将矩阵转换为long
  2. 绑定它们,
  3. 确定重复配对的处理方式(这里我只是取第一个值...)
  4. 然后传播到宽阔的后背。

更新:可能会运行distinct()并在group_by(ind_1, ind_2, value)中添加 value 。加上spread()中的0?

示例:

library(tidyverse)
mat1 <- structure(c(0L, -1L, 1L, 0L, 1L, -1L, 0L, 0L, 0L, 1L, -1L, -1L, 
                    -1L, 0L, 0L, 1L), .Dim = c(4L, 4L), .Dimnames = list(c("Tommy", 
                                                                           "Roy", "Addy", "Sam"), c("Tommy", "Roy", "Addy", "Sam")))
mat2 <- structure(c(0L, -1L, 1L, 0L, 1L, 1L, -1L, 0L, 0L, 0L, 0L, 1L, 
                    -1L, -1L, 0L, -1L, 0L, 0L, 1L, -1L, 0L, 1L, -1L, 0L, 1L), .Dim = c(5L, 
                                                                                       5L), .Dimnames = list(c("Mike", "Roy", "Addy", "Sam", "Dan"), 
                                                                                                             c("Mike", "Roy", "Addy", "Sam", "Dan")))

mat1_l <- mat1 %>% 
  as.data.frame() %>% 
  rownames_to_column(var = "ind_1") %>% 
  as_tibble() %>% 
  gather(ind_2, value, -ind_1)

mat2_l <- mat2 %>% 
  as.data.frame() %>% 
  rownames_to_column(var = "ind_1") %>% 
  as_tibble() %>% 
  gather(ind_2, value, -ind_1)


rbind(mat1_l, mat2_l) %>% 
  group_by(ind_1, ind_2) %>% 
  slice(1) %>% 
  ungroup() %>% 
  spread(ind_2, value)
#> # A tibble: 6 x 7
#>   ind_1  Addy   Dan  Mike   Roy   Sam Tommy
#>   <chr> <int> <int> <int> <int> <int> <int>
#> 1 Addy     -1    -1     1     0     0     1
#> 2 Dan       0     1     1     0    -1    NA
#> 3 Mike      0     0     0     1    -1    NA
#> 4 Roy       1     1    -1    -1     0    -1
#> 5 Sam      -1     0     0     0     1     0
#> 6 Tommy     0    NA    NA     1    -1     0