以参考文献中的示例为基础,mat1
和mat2
的并集使用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")))
答案 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_1
和weight_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)
我会
更新:可能会运行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